Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 每次应用程序启动时都会显示推送通知_Android_Notifications_Push Notification_Google Cloud Messaging - Fatal编程技术网

Android 每次应用程序启动时都会显示推送通知

Android 每次应用程序启动时都会显示推送通知,android,notifications,push-notification,google-cloud-messaging,Android,Notifications,Push Notification,Google Cloud Messaging,我遵循了一个教程,在我现有的应用程序上实现了GCM推送通知。我已将Main活动设置为显示通知。用户收到通知,单击该通知并在Main活动上显示通知。问题在于应用程序重新启动时,每次都会显示通知 主要活动 public void registerGCM() { .... protected Void doInBackground(Void... params) { ServerUtilities.register(context, uname, uemail, reg

我遵循了一个教程,在我现有的应用程序上实现了GCM推送通知。我已将
Main
活动设置为显示通知。用户收到通知,单击该通知并在
Main
活动上显示通知。问题在于应用程序重新启动时,每次都会显示通知

主要活动

public void registerGCM() {
    ....
    protected Void doInBackground(Void... params) {
        ServerUtilities.register(context, uname, uemail, regId);
    }
}
我在
Main
活动的
onCreate
上调用此函数来注册应用程序。这个过程运作良好

以下是onCreate函数之外的
Main
活动中的BoradcaseReceiver代码

private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {
        String newMessage = intent.getExtras().getString(EXTRA_MESSAGE);
        WakeLocker.acquire(getApplicationContext());
        Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
        WakeLocker.release();
    }
};
它应该只在用户单击时显示一次通知(这很好),但在每次使用启动应用程序时也会显示通知。我如何防止这种情况

如果我不够清楚,请告诉我。 我们将非常感谢您的帮助


谢谢

如果我理解正确的话,你会错过这个

将此属性添加到结果活动(即MainActivity)的活动标记之间的清单中(有关更多信息,请查看上面的链接)

我看不到您的notification receiver类,因此请确保将resultIntent标志设置为

resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

注意:别忘了给每个通知一个唯一的id

我感谢您的评论。我会在做了你提到的编辑后再给你回复。谢谢正如你所解释的,我已经编辑了代码,我仍然有同样的问题。下面是更新后的代码:清单:
生成通知:
通知Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK)你能解释一下我如何将唯一id传递给通知吗?你能分享你正在注册并收到通知的完整代码吗?这是我使用的教程。我刚刚将教程的MainActivity部分移到我的应用程序Main Activity。如果你共享你的电子邮件,我会将主要活动代码通过电子邮件发送给你。看不到你的电子邮件,它是公开共享的吗?
resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);