【问题标题】:android GCM push notifications安卓 GCM 推送通知
【发布时间】:2012-10-24 13:35:09
【问题描述】:

我已成功设置代码以在 Android 应用上通过 phonegap 运行 GCM。我设法保护了手机注册 ID,并能够在 PHP 脚本中使用此 ID 向应用程序发送消息。 我唯一的问题是,当应用程序打开时,消息显示为 javascript 警报,我希望将消息发送到手机,但顶部通知栏上没有显示图标。

**> 确实可以在 android 设备的顶部栏上显示图标

有谁知道 Phonegap 的 GCM 插件是否能够做到这一点?**

我使用的是 C2DM-Phonegap。 https://github.com/marknutter/GCM-Cordova

请朋友们帮帮我...

【问题讨论】:

  • 您能否重新表述您的问题以使其更清楚?您是否尝试在收到推送通知时在通知(顶部)栏中显示一个图标?
  • 是的,当收到推送通知时,通知(顶部)栏中不会显示图标
  • toadzky 下面的回答是正确的。大多数库都允许您指定在收到推送通知时要执行的操作。

标签: android cordova push-notification phonegap-plugins android-c2dm


【解决方案1】:

您使用的库指定收到消息时需要执行的操作。你可以修改他们的代码来做你想做的事,或者推出你自己的 GCMIntentService。

请参考此链接:

https://github.com/phonegap-build/PushPlugin

https://build.phonegap.com/plugins/324

【讨论】:

    【解决方案2】:

    您使用的库指定收到消息时需要执行的操作。你可以修改他们的代码来做你想做的事,或者推出你自己的 GCMIntentService。

    【讨论】:

      【解决方案3】:

      最快的方法是将以下代码添加到 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);
      

      【讨论】:

        【解决方案4】:

        请检查此代码

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

        【讨论】:

          猜你喜欢
          • 2017-12-01
          • 1970-01-01
          • 2015-03-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多