安卓实现后台读取通知打开app

1.读取通知

首先在AndroidManifest.xml里声明读取通知权限

<uses-permission
    android:name="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
    tools:ignore="ProtectedPermissions"
/>

同时在AndroidManifest.xml里声明ListenerService

<service
    android:name=".MyNotificationListenerService"
    android:label="@string/app_name"
    android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
    android:exported="false">
    <intent-filter>
        <action android:name="android.service.notification.NotificationListenerService" />
    </intent-filter>
</service>

然后继承NotificationListenerService,编写自己的MyNotificationListenerService

class MyNotificationListenerService : NotificationListenerService() {

    private fun sendBroadcastForNotification(packageName: String?, title: String?, text: String?) {
        val intent = Intent("YOURPACKAGENAME.ACTION_BROADCAST_RECEIVED")
        intent.putExtra("packageName", packageName)
        intent.putExtra("title", title)
        intent.putExtra("text", text)
        Log.d("MyNotificationListenerService", "Received Notification: packageName=$packageName, title=$title, text=$text")
        sendBroadcast(intent)
    }

    override fun onNotificationPosted(sbn: StatusBarNotification?) {
        sbn?.let {
            val packageName = it.packageName
            val notificationTitle = it.notification.extras.getString("android.title")
            val notificationText = it.notification.extras.getString("android.text")
            Log.d("onNotificationPosted", "Received Notification: packageName=$packageName, title=$notificationTitle, text=$notificationText")
            sendBroadcastForNotification(packageName, notificationTitle, notificationText)
        }
    }

    override fun onNotificationRemoved(sbn: StatusBarNotification?) {
        // 可选:处理通知被移除的情况
    }
}

这样,app就能监控所有通知了(需要用户手动为应用开启通知权限)

2.监测到特定通知打开app

首先在AndroidManifest.xml里声明receiver

<receiver android:name=".MyBroadcastReceiver" android:exported="false">
    <intent-filter>
        <action android:name="YOURPACKAGENAME.ACTION_BROADCAST_RECEIVED" />
    </intent-filter>
</receiver>

安卓某版本开始想要在app不在前台时调用startActivity成功,需要满足一些特定条件。在AndroidManifest.xml中添加悬浮窗权限即可。(需要用户手动给应用打开悬浮窗权限。实际上我们并不需要真的有一个悬浮窗,只要有这个权限即可。另外把允许在其他应用上层显示的权限也打开)

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

下面继承BroadcastReceiver编写自己的receiver,来接收前面MyNotificationListenerService发出的broadcast,择机startActivity即可

class MyBroadcastReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        Log.d("BroadcastReceiver", "Received broadcast")
        if (intent?.action == "YOURPAKAGENAME.ACTION_BROADCAST_RECEIVED") {
            val packageName = intent.getStringExtra("packageName")
            val title = intent.getStringExtra("title")
            val text = intent.getStringExtra("text")
            context?.startActivity(launchIntent)
        }
    }
}

别忘了在Activity中注册receiver

val filter = IntentFilter("YOURPACKAGENAME.ACTION_BROADCAST_RECEIVED")
val receiver = MyBroadcastReceiver()
registerReceiver(receiver, filter)

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注