繁体   English   中英

从PendingIntent启动NEW活动,并获得独特的额外收益

[英]start NEW Activity from PendingIntent with unique extra

用户点击通知后,我尝试做“更多”。

我需要什么,以独特的额外内容“ 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条通知,在我第一次单击后,活动开始了,但是第二次通知中有额外的数据,为什么?

该问题是由于使用: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);

因此,原因是您要为通知设置唯一的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中获得唯一的代码

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM