onNotificationPostedPosted

onNotificationPostedPosted

收到PUSH通知后,我创建了一个NotificationListenerService来播放声音。但是NotificationListenerService会通知我有关系统范围内发生的任何通知,并且
我想做的是,限制NotificationListenerService的行为,以便仅响应从我的应用发布的通知。换句话说,回调onNotificationPostedPosted
在以下NLS部分中发布的代码中,即使我将USB电缆插入设备并执行了该回调中的代码,我也会被调用,并且我希望该代码在
“ onNotificationPostedPosted”仅在我的应用发送通知时执行。

为了解决此问题,如下面的代码所示,我在Intent中添加了以下内容:

intent.putExtra("key_message", "MANT-App");


我没有的问题是,我无法在onNotificationPostedPosted中访问该意图。

请让我知道如何在“ onNotificationPostedPosted”中获取/访问IntentPendingIntent吗?

码:

Intent intent = new Intent(getApplicationContext(), HomeActivity_.class);
    intent.putExtra("key_message", "MANT-App");
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    String title = getString(R.string.app_name);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this).setSmallIcon(R.drawable.ic_launcher);
    builder.setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setContentText(msg).setContentTitle(title);
    builder.setContentIntent(contentIntent).setAutoCancel(true);
    builder.setLights(Color.argb(1, 255, 0, 0), 400, 1300);
    builder.setOngoing(true);
    builder.setAutoCancel(true);

    ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE)).notify(NOTIFICATION_ID, builder.build());


NLS

@Override
public void onNotificationPosted(StatusBarNotification sbn) {
    super.onNotificationPosted(sbn);
    Log.w(TAG, "onNotificationPosted");

    Log.d(TAG, "sbn.describeContents(): " + sbn.describeContents());
    Log.d(TAG, "getNotification().contentIntent.describeContents(): " + sbn.getNotification().contentIntent.describeContents());

    Message msg = handler.obtainMessage();
    msg.obj = "onNotificationPosted";
    handler.sendMessage(msg);

    mNotificationRemoved = false;
    if (!NLSNotificationTone11.TONE_IS_PLAYING) {
        NLSNotificationTone11.TONE_IS_PLAYING = true;
        new ThreadPlayTone(this, NLSNotificationTone11.DURATION_OF_RING_TONE).start();
    }
}

最佳答案

PendingIntent pendingIntent = statusBarNotification.getNotification().contentIntent


使用此方法在NotificationListener类中获取API级别18及更高版本的PendingIntent

07-24 09:29