Android PendingIntent无法启动活动

3

我将创建一个使用Google GCM服务的应用程序。

因此,我定义了以下函数:

@Override
protected void onMessage(Context context, Intent intent)

onMessage调用一个函数,这是代码的一部分:

 Intent notificationIntent = new Intent(context,ShowMess.class);
    notificationIntent.putExtra("id",current_id);
    notificationIntent.putExtra("cat", cat);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  //  notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(context, id, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notification.defaults |= Notification.DEFAULT_SOUND;
    notificationManager.notify(id, notification);

如果我使用AsyncTask给自己发送一条消息,但很快关闭应用程序(使用返回按钮),那么下一条消息将不会启动ShowMEss.class活动。你认为为什么会这样?
再见。
谢谢。

PendingIntent intent = PendingIntent.getActivity(context, id, notificationIntent, 0); 尝试使用这段代码。 - Parag Chauhan
1个回答

0

你的代码中没有注册通知。你是否忘记在代码中添加以下部分?

    NotificationManager notificationManager = 
  (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

notificationManager.notify(0, notification); 

抱歉,我漏掉了那部分代码。我刚刚更新了它。 - Edge7

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