如何在NotificationListenerService中检索PendingIntent

时间:2017-07-24 10:40:34

标签: android android-intent android-notifications android-pendingintent notification-listener

我收到PUSH通知后,我创建了NotificationListenerService播放声音。但NotificationListenerService告诉我系统范围内发生的任何通知,以及 我想要做的是,限制NotificationListenerService的行为,以便仅响应从我的应用程序发布的通知。换句话说,回调onNotificationPostedPostedNLS部分的下面发布的代码中,即使我将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();
    }
}

2 个答案:

答案 0 :(得分:1)

PendingIntent pendingIntent = statusBarNotification.getNotification().contentIntent

使用此方法获取API级别18&的PendingIntent。 NotificationListener类

中的值更高

答案 1 :(得分:0)

  

请告诉我如何获取/访问IntentPendingIntent   onNotificationPosted()

你不能。您可以发送PendingIntent,但无法检查它。

但是,您可以获取发布Notification的软件包的软件包名称,这样就可以忽略除您自己的应用程序之外的所有通知。

onNotificationPosted(StatusBarNotification sbn)

if (sbn.getPackageName().equals("my.package.name")) {
    // do stuff
}

编辑:您似乎可以使用从Android 4.2.2开始的反射从Intent获取PendingIntent请执行以下操作:

    PendingIntent pendingIntent = ... // PendingIntent
    try {
        Method method = PendingIntent.class.getDeclaredMethod("getIntent");
        method.setAccessible(true);
        Intent intent = method.invoke(pendingIntent);
        // Here you should have the Intent that is wrapped by the PendingIntent.
        //  You should be able to access the component name, extras,
        //  flags, etc. in the Intent
    } catch (Exception e) {
        // Problem using reflection
    }