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

查看:19
本文介绍了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天全站免登陆