android常駐通知欄點擊事件

 android實現了常駐通知欄的顯示後,需要處理點擊事件。點擊後進入首頁。

1、發現無法收到廣播

1)靜態註冊

2)點擊響應代碼如下

   /**
     * 說明:通知欄點擊事件
     * 作者:耿保龍
     * 添加時間:2020/6/28 17:11
     * 修改人:耿保龍
     * 修改時間:2020/6/28 17:11
     */
    private fun getNotificationPendingIntent(context: Context): PendingIntent? {
        val intent = Intent(BroadcastConfig.ACTION_OPEN_APP_NOTIFICATION)
        return PendingIntent.getBroadcast(context, mNotificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT)
    }

 

此時,無論如何都收不到廣播。

通常,有兩種方式構造自定義廣播

a:      

val intent = Intent(BroadcastConfig.ACTION_OPEN_APP_NOTIFICATION)

b:      

val intent = Intent(context, NotificationReceiver::class.java)
intent.action = BroadcastConfig.ACTION_OPEN_APP_NOTIFICATION

總結:

如果廣播是動態註冊,用第一種(action的方式)可以響應通知欄點擊事件,用第二種(class的方式)構造函數生成Intent,就無法響應。

如果廣播是靜態註冊,第二種方式纔可以響應。

因爲我使用了靜態註冊,而構造自定義廣播時使用的是action方式,所以會收不到廣播。

解決:

還是使用靜態註冊,但是構造方式改爲

    private fun getNotificationPendingIntent(context: Context): PendingIntent? {
        val intent = Intent(context, NotificationReceiver::class.java)
        intent.action = BroadcastConfig.ACTION_OPEN_APP_NOTIFICATION
        return PendingIntent.getBroadcast(context, mNotificationId, intent, PendingIntent.FLAG_CANCEL_CURRENT)
    }

至此,無法收到點擊通知廣播的問題解決!!!

 

2、收到廣播後跳轉報錯

2020-06-29 10:25:42.399 6617-6617/xxx E/AndroidRuntime: FATAL EXCEPTION: main
    Process: xxx, PID: 6617
    java.lang.RuntimeException: Unable to start receiver com.xxx.module.notification.NotificationReceiver: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3460)
        at android.app.ActivityThread.access$1300(ActivityThread.java:202)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1691)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:207)
        at android.app.ActivityThread.main(ActivityThread.java:6878)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876)
     Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
        at android.app.ContextImpl.startActivity(ContextImpl.java:922)
        at android.app.ContextImpl.startActivity(ContextImpl.java:898)
        at android.content.ContextWrapper.startActivity(ContextWrapper.java:389)
        at android.content.ContextWrapper.startActivity(ContextWrapper.java:389)
        at com.xxx.home.HomePageActivity$Companion.startActivity(HomePageActivity.kt:50)
        at com.xxx.module.notification.NotificationReceiver.dealWithResidentNotificationClick(NotificationReceiver.kt:36)
        at com.xxx.module.notification.NotificationReceiver.onReceive(NotificationReceiver.kt:18)
        at android.app.ActivityThread.handleReceiver(ActivityThread.java:3451)
        at android.app.ActivityThread.access$1300(ActivityThread.java:202) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1691) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:207) 
        at android.app.ActivityThread.main(ActivityThread.java:6878) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:876) 

提示的很明顯了,在Activity類型context之外調用startActivity,需要添加flag------

Intent.FLAG_ACTIVITY_NEW_TASK

至此,點擊常駐通知欄可以正常跳轉!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章