安卓GCM推送通知

3
我已经成功设置了代码,在Android应用程序上通过phonegap运行GCM。我已经成功获取了手机的注册ID,并且可以使用这个ID在PHP脚本中向应用程序发送消息。我的唯一问题是,当应用程序打开时,消息显示为javascript警报,并且我希望将消息发送到手机上,但不显示在顶部通知栏上的图标。
> 是否可能在Android设备上显示顶部栏上的图标?
有人知道Phonegap的GCM插件是否能够做到这一点吗?
我一直在使用C2DM-Phonegap。 https://github.com/marknutter/GCM-Cordova 请帮帮我,朋友们...

你能否重新表达你的问题以使其更清晰明了?你是想在接收到推送通知时,在通知(顶部)栏中显示一个图标吗? - acj
是的,当收到推送通知时,在通知(顶部)栏中不显示图标。 - Patel
toadzky的下面的回答是正确的。大多数库都允许您指定在接收到推送通知时执行的操作。 - acj
4个回答

4

2

你使用的库规定了接收到消息时要执行的操作。你可以修改他们的代码以实现你想要的功能,或者自己编写GCMIntentService。


0
最快的方法是将以下代码添加到GCMIntentService.java文件中的onMessage函数中。您正在使用的GCM插件仅接收GCM消息,但您必须自己触发通知。您还可以尝试使用状态栏通知的cordova插件。
 protected void onMessage(Context context, Intent intent) {
Log.d(TAG, "onMessage - context: " + context);

// Extract the payload from the message
Bundle extras = intent.getExtras();
if (extras != null) {
    String message = extras.getString("message");
    String title = extras.getString("title");

    Notification notif = new Notification(R.drawable.ic_launcher, message, System.currentTimeMillis() );
    notif.flags = Notification.FLAG_AUTO_CANCEL;
    notif.defaults |= Notification.DEFAULT_SOUND;
    notif.defaults |= Notification.DEFAULT_VIBRATE;

    Intent notificationIntent = new Intent(context, SomeActivity.class);
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);

    notif.setLatestEventInfo(context, "Title of notification", message, contentIntent);
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);
    mNotificationManager.notify(1, notif);

0
请检查这段代码:

private static void generateNotification(Context context, String message) { int icon = R.drawable.ic_launcher;

       // here is your icon drawable instead of ic_launcher

    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);
    Intent notificationIntent = new Intent(context.getApplicationContext(), devicephp.class);
    // set intent so it does not start a new activity

    notificationIntent.putExtra("msg", message);
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(0, notification);


}

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