Android - GCM推送通知不会出现在通知列表中 [英] Android - GCM push notifications not appearing in notifications list

查看:102
本文介绍了Android - GCM推送通知不会出现在通知列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理我的第一个Android应用,以便使用Google Cloud Messaging(GCM)服务进行推送通知。我已经到了可以从我的服务器应用程序成功发送消息的地步,并将消息的内容记录在客户端应用程序的GCMIntentService类中的onMessage事件中。但是我没有在设备上看到收到消息的任何可视指示。我正在等待消息出现在手机上的下拉通知列表中,就像它在iPhone上一样。这是否必须手动编码?还有一种显示消息的常用方法,无论当前哪个活动处于活动状态,并且该应用程序在后台是否处于空闲状态?任何帮助表示赞赏。

I'm working on my first Android app to use the Google Cloud Messaging (GCM) service for push notifications. I've got to the point where I can successfully send a message from my server application, and log the content of the message in the onMessage event within my GCMIntentService class on the client app. However I don't see any visual indication on the device that a message was received. I was expecting the message to appear in the pull-down notifications list on the phone, as it does on the iPhone. Does this have to be coded manually? Also is there a common method for displaying the message regardless of which activity is currently active, and if the app is idle in the background? Any help appreciated.

推荐答案

此代码会在屏幕顶部的android系统栏中生成通知。此代码将创建一个新的意图,在用户点击顶部栏中的通知之后,该意图将引导用户转到Home.class。如果您希望根据当前活动执行特定的操作,您可以将GCMIntentService的广播请求发送到其他活动。

This code will generate a notification in the android system bar at the top of the screen. This code will create a new intent that will direct the user to a "Home.class" after clicking on the notification in the top bar. If you would like it to do something specific based on the current activity you could send broadcast requests from the GCMIntentService to your other activities.

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中的资源,需要出席工作,但它应该给你的想法。有关状态通知的更多信息,请参见 http://developer.android.com /guide/topics/ui/notifiers/index.html 和关于广播接收者的信息。 http://developer.android.com/reference/android/content/BroadcastReceiver.html

Note that this example uses resources in R.drawable and R.String that will need to be present to work but it should give you the idea. See this for more information about status notifications http://developer.android.com/guide/topics/ui/notifiers/index.html and this about broadcast recievers. http://developer.android.com/reference/android/content/BroadcastReceiver.html

这篇关于Android - GCM推送通知不会出现在通知列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆