在安卓系统中,PendingIntent不一定总是启动。

4

我在收到GCM推送通知消息后创建通知。一切都完成了。但是当我点击通知时,有时它无法启动(PendingIntent)。大多数情况下,问题出现在我接收到通知后不久点击时。PendingIntent实际上是我的第一个登录页面。这是源代码...

public void createNotification(Context context, String message) {
    NotificationManager notificationManager = (NotificationManager) context
            .getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(R.drawable.icon,
            "My notification", System.currentTimeMillis());
    // Hide the notification after its selected
    notification.flags |= Notification.FLAG_AUTO_CANCEL ;
    Intent intent;


    intent = new Intent(context, Login.class);
    intent.putExtra("pushNoti", "pushNoti");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    notification.setLatestEventInfo(context, "my Alert",
            message, pendingIntent);

    notification.defaults = Notification.DEFAULT_SOUND;
    notificationManager.notify(0, notification);

}

这里的上下文是从GCM onMessage(Context context,Intent intent)中获取的。

有人能帮我吗?


同时,当我这样做时,我的应用程序在后台运行。 - hacker
1个回答

0
notificationManager.notify(0, notification);

来自文档:

public void notify (int id, Notification notification)

自:API 级别 1 将通知发布到状态栏中显示。如果您的应用程序已经发布了具有相同 ID 的通知,并且尚未被取消,则会使用更新的信息替换它。

参数

id:在您的应用程序中唯一的此通知标识符。

notification:描述要向用户显示的内容的 Notification 对象。不能为空。

请尝试使用每个应用程序内唯一的标识符,而不是 0。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接