Java Android通知持续启动应用程序
我正在创建通知,收到通知后,应用程序将启动。我的问题是如何阻止这种情况发生Java Android通知持续启动应用程序,java,android,notifications,push-notification,Java,Android,Notifications,Push Notification,我正在创建通知,收到通知后,应用程序将启动。我的问题是如何阻止这种情况发生 final private static void generateNotification(Context context, String message){ Intent notificationIntent = new Intent(context, MainActivity.class); notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR
final private static void generateNotification(Context context, String message){
Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(context.getString(R.string.app_name))
.setContentIntent(intent)
.setContentText(message)
.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(0, mBuilder.build());
}
通知在GCMinentService中调用:
@Override
protected void onMessage(Context context, Intent intent) {
Log.i(TAG, "Received message");
String response = intent.getExtras().getString("response");
switch(response){
case "logout":
String message = "You logged into another device!";
//displayMessage(context, message);
// notifies user
generateNotification(context, message);
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
break;
default:
break;
}
}
只是不要做这些我已经在下面注释掉的行。该代码在收到的
“注销”
消息上启动活动
generateNotification(context, message);
//Intent i = new Intent(context, MainActivity.class);
//i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//startActivity(i);
“收到后,应用程序将启动”,你确定吗?单击时没有?是的,即使我没有单击通知,应用程序也会启动。此代码从何处调用,因为我认为问题不在这里。您是否有服务和广播接收器反复启动?如果是这样的话,那就是问题所在problem@Jasz我没有广播接收器:SExcellent!这是可行的,但是现在我如何在通知点击或应用程序打开时运行它?你试过点击它吗?由于您的代码似乎有这样的功能,尽管使用
setContentIntent
As for is-app-open:perfect:)正是我所需要的。谢谢