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

至此,点击常驻通知栏可以正常跳转!!!

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