如果应用程序在移动设备中关闭,则GCM推送通知(Phonegap Android)

时间:2015-04-04 09:42:23

标签: javascript android jquery cordova phonegap-pushplugin

我在我的phonegap安卓应用中使用GCM。我面临的问题是,如果应用程序在移动设备中打开,则该应用程序能够接收通知,但如果已关闭则无法接收通知。我已经通过编写的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);

         }
         }

1 个答案:

答案 0 :(得分:0)

我认为你正在使用GCMPlugin插件。当应用程序进入后台或被销毁时,无处可写如何处理推送通知。我认为最好删除此插件并使用{{3} ,否则你不仅要对GCMIntentservice.java做一个重大改变,还要对GCMPlugin.java做一个大的改动。