GCM推送通知,如果应用程序在手机中关闭(Phonegap Android)

GCM Push notification if App is closed in mobile (Phonegap Android)

本文关键字:Phonegap Android 手机 通知 应用程序 如果 GCM      更新时间:2023-09-26

我正在将GCM与我的phonegap android应用程序一起使用。我面临的问题是,如果应用程序在手机中打开,它可以接收通知,但如果关闭,它就无法接收通知。我已经完成了java代码的编写,运行良好。但它无法与为接收消息而编写的javascript代码进行通信

           public class GCMIntentService extends GCMBaseIntentService {
             public static final String ME="GCMReceiver";
             public GCMIntentService() {
              super("GCMIntentService");
                }
            private static final String TAG = "GCMIntentService";
             @Override
             public void onRegistered(Context context, String regId) {
               Log.v(ME + ":onRegistered", "Registration ID arrived!");
               Log.v(ME + ":onRegistered", regId);
               JSONObject json;
                 try
                  {
                json = new JSONObject().put("event", "registered");
               json.put("regid", regId);
               Log.v(ME + ":onRegisterd", json.toString());
             // In this case this is the registration ID
              GCMPlugin.sendJavascript( json );
                   }
                catch( JSONException e)
                        {
                       // No message to the user is sent, JSON failed
                        Log.e(ME + ":onRegisterd", "JSON exception");
                   }
                }
              @Override
              public void onUnregistered(Context context, String regId) {
                Log.d(TAG, "onUnregistered - regId: " + regId);
               }
             @Override
            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) {
                     try
                       {
                        Log.v(ME + ":onMessage extras ",   extras.getString("message"));                                      
                     JSONObject json;
                     json = new JSONObject().put("event", "message");

             json.put("message", extras.getString("message"));
                json.put("msgcnt", extras.getString("msgcnt"));
               Log.v(ME + ":onMessage ", json.toString());
           GCMPlugin.sendJavascript( json );
             // Send the MESSAGE to the Javascript application
            }
         catch( JSONException e)
        {
             Log.e(ME + ":onMessage", "JSON exception");
              }         
         }

           }
              @Override
              public void onError(Context context, String errorId) {
             Log.e(TAG, "onError - errorId: " + errorId);
         }
         }

我认为您使用的是GCMPlugin插件。当应用程序进入后台或被销毁时,没有任何关于如何处理推送通知的内容。在我看来,最好删除这个插件并使用PushPlugin,否则你不仅要对GCMIntentservice.java,还要对GCMPlugin.java进行重大更改。