简体   繁体   English

Android - GCM推送通知未出现在通知列表中

[英]Android - GCM push notifications not appearing in notifications list

I'm working on my first Android app to use the Google Cloud Messaging (GCM) service for push notifications. 我正在开发我的第一个Android应用,以使用Google云消息传递(GCM)服务进行推送通知。 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. 我已经到了可以从我的服务器应用程序成功发送消息的位置,并在客户端应用程序上的GCMIntentService类中的onMessage事件中记录消息的内容。 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. 我期待这条消息出现在手机的下拉通知列表中,就像在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. 任何帮助赞赏。

This code will generate a notification in the android system bar at the top of the screen. 此代码将在屏幕顶部的android系统栏中生成通知。 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. 此代码将创建一个新的intent,在单击顶部栏中的通知后将用户定向到“Home.class”。 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. 如果您希望它根据当前活动执行某些特定操作,您可以将GCMIntentService的广播请求发送到您的其他活动。

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);
}

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. 请注意,此示例使用R.drawable和R.String中的资源,这些资源需要存在才能工作,但它应该为您提供想法。 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/guide/topics/ui/notifiers/index.html以及有关广播接收器的详细信息,请参阅此处。 http://developer.android.com/reference/android/content/BroadcastReceiver.html http://developer.android.com/reference/android/content/BroadcastReceiver.html

If you are using GcmListenerService you can use this code, add to your onMessageReceived the sendNotification() 如果您正在使用GcmListenerService,则可以使用此代码,将onMessageReceived添加到sendNotification()

@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());
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM