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

6
我正在开发我的第一个 Android 应用程序,使用 Google 云消息传递(GCM)服务进行推送通知。我已经成功地从我的服务器应用程序发送了一条消息,并在客户端应用程序的 GCMIntentService 类中的 onMessage 事件中记录了消息的内容。然而,我没有在设备上看到任何视觉指示表明已收到消息。我原以为消息会出现在下拉通知列表上,就像 iPhone 上那样。这需要手动编码吗?此外,是否有一种常见的方法可以显示消息,无论当前正在激活哪个活动,并且如果应用程序在后台处于空闲状态?谢谢您的帮助。
2个回答

7

这段代码将在安卓系统屏幕顶部生成一个通知。用户点击通知后,该代码将创建一个新的意图(intent),将用户重定向到“Home.class”。如果您希望根据当前活动(activity)执行特定的操作,您可以从GCMIntentService发送广播请求到其他活动(activity)。

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

你好,我使用了这个插件https://github.com/marknutter/GCM-Cordova并按照所有的说明进行了操作。然而,只有当我在应用程序内部时才能收到消息,但无法让应用程序在Android通知区域显示通知。请给予建议。我使用此插件是因为我使用PhoneGap@Zachary Moshansky。 - Sir Lojik
抱歉,我从未使用过Phonegap,并且无法评论如何使通知与之配合工作。 - Zachary Moshansky
@ZacharyMoshansky。我正在尝试开发一个需要推送通知服务的应用程序...我可以接收到通知,如果它们被点击,可以看到它们。问题在于,如果我在具有(Android 4.4 KitKat)的Google Nexus上测试应用程序...它也会收到通知,但是单击它们不会打开所需的Activity。这与意图标志有关吗? - Saty
没事,我刚刚尝试在同一台手机上调试应用程序,遇到了“Permission Denial:starting Intent”异常,并解决了它... - Saty

1
如果您正在使用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());
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接