单击通知时返回json对象

时间:2013-01-04 21:23:00

标签: android cordova google-cloud-messaging

我正在构建一个支持使用phonegap / cordova和plugin推送通知的Android应用。该插件有一个onMessage()方法,只要收到推送通知就会触发该方法,此方法将json对象返回给我的javascript插件。但是,我希望此方法仅在单击状态栏上的通知时返回json。

这是我的onMessage()方法:

protected void onMessage(Context context, Intent intent) {
    String message = "", title = "", type = "";
    int msgctn = 0, schoolid = 0, studentid = 0, contentid = 0;

    // Extract the payload from the message
    Bundle extras = intent.getExtras();
    if (extras != null) {
      try
      {
          message = extras.getString("message");
          title = extras.getString("title");
          type = extras.getString("type");
          msgctn = Integer.parseInt(extras.getString("msgctn"));
          schoolid = Integer.parseInt(extras.getString("schoolid"));
          studentid = Integer.parseInt(extras.getString("studentid"));
          contentid = Integer.parseInt(extras.getString("contentid"));

        JSONObject json;
        json = new JSONObject().put("event", "message");

        json.put("message", message);
        json.put("type", type);
        json.put("contentid", contentid);
        json.put("schoolid", schoolid);
        json.put("studentid", studentid);

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

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

      NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
          .setContentText(message)
          .setContentTitle(title)
          .setSmallIcon(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? R.drawable.icon : android.R.drawable.ic_dialog_info)
          .setAutoCancel(true)
          .setContentIntent(contentIntent)
          .setWhen(System.currentTimeMillis())
          //.setDefaults(Notification.DEFAULT_ALL)
          .setLights(-16711681, 2000, 1000)
          .setNumber(msgctn)
          .setSound(Uri.parse("android.resource://"+ getPackageName() + "/" + R.raw.notifsound));

      Notification notification = builder.build();
      NotificationManager notificationManager = getNotificationManager(context);
      notificationManager.notify(1, notification);

    }

我必须创建一个通知对象才能在状态栏上显示它。当点击通知它调用意图App4时,这显示我的应用程序的index.html,但我想执行一些javascript函数或使用有效负载将一些json对象返回到我的应用程序。也许我可以调用其他函数来做这个,而不是一个意图,但我是android世界的新手,不知道该怎么做。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:1)

您可以做的是将JSON数据作为额外内容放入您正在创建的notificationIntent Intent中。您可以通过调用notificationIntent的putExtras()方法来执行此操作,也可以手动执行此操作。无论您采用哪种方式,都需要将所需数据作为附加内容提供给notificationIntent。

然后,当您的App4活动开始时,您需要在onNewIntent()onCreate()中捕获Intent并检查Intent或Bundle是否具有您想要的额外内容。如果是,则创建JSON对象并将其传递给javascript应用程序,就像在粘贴的onMessage()方法中一样。

您还必须删除在onMessage()方法中将JSON对象发送到javascript应用程序的代码。

我希望有帮助,它应该可以工作,但我离开了我的电脑,实际上无法测试代码。