【问题标题】:start NEW Activity from PendingIntent with unique extra从 PendingIntent 开始新的活动与独特的额外
【发布时间】:2015-07-14 05:49:37
【问题描述】:

我尝试在用户点击通知后“阅读更多”。

我需要什么,用独特的额外内容“id”、“title”和“text”开始新的 Activity。

我的代码:

void notificationSend(String id, String bar, String title, String text, String image, int delay) {
    Bitmap bitmap = getBitmapFromURL(image, 130);

    if(bitmap == null){
        bitmap = getBitmapFromURL("https://pp.vk.me/c621316/v621316641/119d5/HB9s2z5mX-s.jpg", 130);
    }

    Intent notificationIntent = new Intent(this, SingleNewsActivity.class);
    notificationIntent.putExtra("id", id);
    notificationIntent.putExtra("title", title);
    notificationIntent.putExtra("text", text);

    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    builder.setContentIntent(contentIntent);
    builder.setSmallIcon(R.drawable.ic_bitcoin_notification);
    builder.setLargeIcon(bitmap);
    builder.setTicker(bar);
    builder.setContentTitle(title);
    builder.setContentText(text);
    builder.setWhen(System.currentTimeMillis());
    builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));


    Notification notification = builder.build();
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    try {
        notificationManager.notify(new Integer(id), notification);
        sleep(2000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

然后它会做接下来的事情,让我们发送 2 个通知,在我点击第一个后,活动开始但第二个通知有额外的数据,为什么?

【问题讨论】:

标签: java android android-intent notifications android-pendingintent


【解决方案1】:

问题是由于使用:FLAG_UPDATE_CURRENT

如果您看到此标志的文档:

Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).
This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.

在您的情况下,您只是在更新额外内容,因此,额外内容的值会更新。

为了解决您的问题,您应该为所有情况传递唯一的请求代码:

当前代码:

PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

实际:

PendingIntent.getActivity(this, unique_code, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

【讨论】:

    【解决方案2】:

    所以原因是您为通知设置了唯一 id,假设当您的第一个通知到来时 id 值为 1,然后使用该 id 创建通知对象,当您第二个通知到来时,您的 id 再次为 1 所以您的通知已更新,当您单击它时,您会收到第二个通知。 您可以做的是为通知设置不同的ID。看看下面的代码

    private NotificationManager mNotificationManager;
    private NotificationCompat.Builder builder;
    

    在函数中编写以下代码

     mNotificationManager = (NotificationManager) this
                .getSystemService(Context.NOTIFICATION_SERVICE);
        Random random = new Random();
        int NOTIFICATION_ID = random.nextInt(9999 - 1000) + 1000;
        Intent intent = new Intent(this, SingleNewsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putExtra("id", id);
        bundle.putExtra("title", title);
        bundle.putExtra("text", text);
    
        intent.putExtras(bundle);
    
        PendingIntent contentIntent = PendingIntent.getActivity(this, NOTIFICATION_ID,
                intent, 0);
    
    
        NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                this).setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("TITLE")
                .setStyle(new NotificationCompat.BigTextStyle().bigText("ASD"))
                .setContentText("Summary").setAutoCancel(true);
    
        mBuilder.build().flags |= Notification.FLAG_AUTO_CANCEL;
        mBuilder.setContentIntent(contentIntent);
    
        mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    
        Log.d("", "Notification sent suessfully.");
    

    在上述代码中,每当您收到通知时,您都会在 NOTIFICATION_ID

    中获得一个唯一代码

    【讨论】:

      猜你喜欢
      • 2014-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-14
      • 1970-01-01
      相关资源
      最近更新 更多