Java 我的通知只在应用程序打开时有效,为什么?
我有一个从GCM接收推送的应用程序,我将其配置为显示特定图标和动作,但它只在应用程序打开时发生;当我杀死它或关闭应用程序时,通知会显示另一条消息和另一个图标(在Java中,该图标设置在服务器端),我只想使用应用程序的代码版本通知 这是onReceive代码:Java 我的通知只在应用程序打开时有效,为什么?,java,android,notifications,google-cloud-messaging,push,Java,Android,Notifications,Google Cloud Messaging,Push,我有一个从GCM接收推送的应用程序,我将其配置为显示特定图标和动作,但它只在应用程序打开时发生;当我杀死它或关闭应用程序时,通知会显示另一条消息和另一个图标(在Java中,该图标设置在服务器端),我只想使用应用程序的代码版本通知 这是onReceive代码: NotificationManager notificationManager = (NotificationManager) context .getSystemService(Context.NOTIFI
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(context, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
intent, 0);
Notification.Builder nb = new Notification.Builder(context);
nb.setContentIntent(pendingIntent);
nb.setContentTitle("Nova aprovacao cadastrada!");
nb.setContentText(message.getAlert());
nb.setSmallIcon(R.drawable.ic_launcher);
Notification notification = nb.build();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults |= Notification.DEFAULT_LIGHTS;
notificationManager.notify(R.drawable.ic_launcher, notification);
ic_启动器是我想展示的图标,但正如我所说,它只在应用程序打开时才起作用
在清单中,我提出:
<receiver
android:name="..."
android:permission="..."
android:exported="true" >
请忽略“…”
更新说明
我在这里读到一篇文章,当你关闭你的应用程序时,广播接收器会继续运行。但在我的情况下,我没有这个类,也没有使用它。我的onReceive在关闭时如何保持运行
- 问题:当应用程序关闭时,通知无法像我预期的那样工作
- 问题:如何仅使用Android代码修复此问题
谢谢 尝试将sendNotification方法中的最后一行更改为
notificationManager.notify(0/*ID of notification*/,notification)代码>@mikebertian它不起作用,反正是thx;)您是否在使用城市飞艇等推送通知服务提供商?是的。就像你说的你需要使用广播接收器。活动生命周期将给您带来各种问题。在自定义推送服务提供商中,您可以选择在向服务提供商注册应用程序时修改默认推送通知,可能是在Application controller中。这样,当您的应用程序未处于运行状态时,您将以您想要的方式获得通知。