Pending Intent没有收到不同notificationId的putExtraBudle

时间:2016-10-21 10:46:55

标签: android notifications android-pendingintent

enter image description here我使用的是PingService.java代码https://github.com/android/platform_development/blob/master/samples/training/notify-user/src/com/example/android/pingme/PingService.java PingService.Java

/**
  * PingService creates a notification that includes 2 buttons: one to snooze the
  * notification, and one to dismiss it.
  */

public class PingService extends IntentService {

private NotificationManager mNotificationManager;
private String mMessage;
private int mMillis;
NotificationCompat.Builder builder;

public PingService() {

    // The super call is required. The background thread that IntentService
    // starts is labeled with the string argument you pass.
    super("com.example.android.pingme");
}

@Override
protected void onHandleIntent(Intent intent) {
    // The reminder message the user set.
    mMessage = intent.getStringExtra(CommonConstants.EXTRA_MESSAGE);
    // The timer duration the user set. The default is 10 seconds.
    mMillis = intent.getIntExtra(CommonConstants.EXTRA_TIMER,
            CommonConstants.DEFAULT_TIMER_DURATION);
    NotificationManager nm = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);

    String action = intent.getAction();
    // This section handles the 3 possible actions:
    // ping, snooze, and dismiss.
    if(action.equals(CommonConstants.ACTION_PING)) {
        issueNotification(intent, mMessage);
    } else if (action.equals(CommonConstants.ACTION_SNOOZE)) {
        nm.cancel(CommonConstants.NOTIFICATION_ID);
        Log.d(CommonConstants.DEBUG_TAG, getString(R.string.snoozing));
        // Sets a snooze-specific "done snoozing" message.
        issueNotification(intent, getString(R.string.done_snoozing));

    } else if (action.equals(CommonConstants.ACTION_DISMISS)) {
        nm.cancel(CommonConstants.NOTIFICATION_ID);
    }
}

private void issueNotification(Intent intent, String msg) {
    mNotificationManager = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);

    // Sets up the Snooze and Dismiss action buttons that will appear in the
    // expanded view of the notification.
    Intent dismissIntent = new Intent(this, PingService.class);
    dismissIntent.setAction(CommonConstants.ACTION_DISMISS);
    PendingIntent piDismiss = PendingIntent.getService(this, 0, dismissIntent, 0);

    Intent snoozeIntent = new Intent(this, PingService.class);
    snoozeIntent.setAction(CommonConstants.ACTION_SNOOZE);
    PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);

    // Constructs the Builder object.
    builder =
            new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_notification)
            .setContentTitle(getString(R.string.notification))
            .setContentText(getString(R.string.ping))
            .setDefaults(Notification.DEFAULT_ALL) // requires VIBRATE permission
            /*
             * Sets the big view "big text" style and supplies the
             * text (the user's reminder message) that will be displayed
             * in the detail area of the expanded notification.
             * These calls are ignored by the support library for
             * pre-4.1 devices.
             */
            .setStyle(new NotificationCompat.BigTextStyle()
                 .bigText(msg))
            .addAction (R.drawable.ic_stat_dismiss,
                    getString(R.string.dismiss), piDismiss)
            .addAction (R.drawable.ic_stat_snooze,
                    getString(R.string.snooze), piSnooze);

    /*
     * Clicking the notification itself displays ResultActivity, which provides
     * UI for snoozing or dismissing the notification.
     * This is available through either the normal view or big view.
     */
     Intent resultIntent = new Intent(this, ResultActivity.class);
     resultIntent.putExtra(CommonConstants.EXTRA_MESSAGE, msg);
     resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

     // Because clicking the notification opens a new ("special") activity, there's
     // no need to create an artificial back stack.
     PendingIntent resultPendingIntent =
             PendingIntent.getActivity(
             this,
             0,
             resultIntent,
             PendingIntent.FLAG_UPDATE_CURRENT
     );

     builder.setContentIntent(resultPendingIntent);
     startTimer(mMillis);
}

private void issueNotification(NotificationCompat.Builder builder) {
    mNotificationManager = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);
    // Including the notification ID allows you to update the notification later on.
    mNotificationManager.notify(CommonConstants.NOTIFICATION_ID, builder.build());
}


  private void startTimer(int millis) {
    Log.d(CommonConstants.DEBUG_TAG, getString(R.string.timer_start));
    try {
        Thread.sleep(millis);

    } catch (InterruptedException e) {
        Log.d(CommonConstants.DEBUG_TAG, getString(R.string.sleep_error));
    }
    Log.d(CommonConstants.DEBUG_TAG, getString(R.string.timer_finished));
    issueNotification(builder);
}
 }
 Intent mServiceIntent = new Intent(context.getApplicationContext(),             PingService.class);
    mServiceIntent.putExtra(Const.EXTRA_MESSAGE, "" + msg);
    mServiceIntent.putExtra(Const.ID, reminder_id);
    startService(mServiceIntent);

将数据传递给Action Button的intentNotification方法,如

   Intent dismissIntent = new Intent(this, PingService.class);
    dismissIntent.setAction(CommonConstants.ACTION_DISMISS);
    dismissIntent.putExtra(CommonConstants.ID, reminder_id);
    PendingIntent piDismiss = PendingIntent.getService(this, 0,   dismissIntent, 0);

    Intent snoozeIntent = new Intent(this, PingService.class);
    snoozeIntent.setAction(CommonConstants.ACTION_SNOOZE);
    snoozeIntent.putExtra(CommonConstants.ID, reminder_id);
    PendingIntent piSnooze = PendingIntent.getService(this, 0, snoozeIntent, 0);

但我没有从issueNotification的Action按钮的Click事件获取ID并在onHandleIntent方法中传递intent

通知生成后自动设置为1,由于错误的通知ID设置为PendingIntent的putExtra,我无法取消该通知

我正在尝试传递不同的标志,如

 PendingIntent piDismiss = PendingIntent.getService(this, 0, taken_intent,     PendingIntent.FLAG_UPDATE_CURRENT);

请帮助我,我很长时间尝试它但没有得到正确的ID。 我的putExtra以某种方式覆盖了1 ....

先谢谢你

1 个答案:

答案 0 :(得分:1)

经过这么多的重新研究后,终于得到了一个解决方案,我需要在待处理的意图中传递不同的代码,如

PendingIntent piDismiss = PendingIntent.getBroadcast(this, CommonConstants.NOTIFICATION_ID,   dismissIntent, 0);

在每个Action Button案例中都是动态的并且是唯一的。 使用广播接收器实现此目的而不是服务(getBroadcast)。

谢谢。