Java Android-GCM推送通知未出现在通知列表中
我正在开发我的第一个Android应用程序,使用谷歌云消息(GCM)服务推送通知。我已经能够成功地从服务器应用程序发送消息,并在客户端应用程序的gcminentservice类中的onMessage事件中记录消息内容。但是,我没有在设备上看到任何接收到消息的视觉指示。我希望这条消息会出现在手机的下拉通知列表中,就像它在iPhone上一样。是否必须手动对其进行编码?此外,是否有一种通用的方法来显示消息,而不管当前活动是什么,以及应用程序在后台是否处于空闲状态?感谢您的帮助。此代码将在屏幕顶部的android系统栏中生成通知。此代码将创建一个新的意图,该意图将在单击顶部栏中的通知后将用户指向“Home.class”。如果您希望它根据当前活动执行某些特定的操作,您可以从GCMinentService向其他活动发送广播请求Java Android-GCM推送通知未出现在通知列表中,java,android,notifications,push-notification,google-cloud-messaging,Java,Android,Notifications,Push Notification,Google Cloud Messaging,我正在开发我的第一个Android应用程序,使用谷歌云消息(GCM)服务推送通知。我已经能够成功地从服务器应用程序发送消息,并在客户端应用程序的gcminentservice类中的onMessage事件中记录消息内容。但是,我没有在设备上看到任何接收到消息的视觉指示。我希望这条消息会出现在手机的下拉通知列表中,就像它在iPhone上一样。是否必须手动对其进行编码?此外,是否有一种通用的方法来显示消息,而不管当前活动是什么,以及应用程序在后台是否处于空闲状态?感谢您的帮助。此代码将在屏幕顶部的an
Intent notificationIntent=new Intent(context, Home.class);
generateNotification(context, message, notificationIntent);
private static void generateNotification(Context context, String message, Intent notificationIntent) {
int icon = R.drawable.icon;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = context.getString(R.string.app_name);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =PendingIntent.getActivity(context, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
}
请注意,这个示例使用了R.drawable和R.String中的资源,这些资源需要存在才能正常工作,但它应该能让您了解这一点。有关状态通知和广播接收器的更多信息,请参见此 如果您使用的是GcmListenerService,则可以使用此代码,将sendNotification()添加到onMessageReceived中
您好,我已经使用了这个插件,并遵循了所有说明。但是,我只能在应用程序中获取消息,但无法让应用程序在androids通知区域中显示通知。请告知。我之所以使用这个插件,是因为我使用了phonegap@Zachary MoshanskyI,很抱歉,我从未使用过phonegap,因此无法评论如何使用通知it@ZacharyMoshansky. 我正在尝试开发一个需要推送通知服务的应用程序。我正在接收通知,如果单击它们,可以看到它们。如果我在谷歌Nexus(Android 4.4 KitKat)上测试该应用程序,就会出现问题。它也会收到通知,但单击它们不会打开所需的活动。这和意图标志有什么关系吗?没关系,我只是试着在同一部手机上调试应用程序,但遭到拒绝:启动意图异常并解决了它。。。。
@Override
public void onMessageReceived(String from, Bundle data) {
String message = data.getString("message");
sendNotification(message);
}
private void sendNotification(String message) {
Intent intent = new Intent(this, YOURCLASS.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_park_notification)
.setContentTitle("Ppillo Message")
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}