如何在BroadcastReceiver中接收解析通知的消息

时间:2014-07-26 14:15:35

标签: android parse-platform

如何在BroadcastReceiver中接收解析通知消息? 我想通过解析通知显示通知的标题:

package com.parse.tutorials.pushnotifications;


public class RetrieveNotify extends BroadcastReceiver {
    private static final String TAG = "RetrieveNotify";

      @Override
      public void onReceive(Context context, Intent intent) {
        try {
          String action = intent.getAction();
          if(action.equals(TAG)){


          String channel = intent.getExtras().getString("com.parse.Channel");
          JSONObject json = new JSONObject(intent.getExtras().getString("com.parse.Data"));

          Log.e(TAG, "got action " + action + " on channel " + channel + " with:");
          Iterator itr = json.keys();
          while (itr.hasNext()) {
            String key = (String) itr.next();
            Log.e(TAG, "..." + key + " => " + json.getString(key));
           }
          }
        } catch (JSONException e) {
          Log.e(TAG, "JSONException: " + e.getMessage());
        }
      }
    }

Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.parse.tutorials.pushnotifications"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="17" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

    <permission android:name="com.parse.tutorials.pushnotifications.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.parse.tutorials.pushnotifications.permission.C2D_MESSAGE" />

    <application
    android:name="com.parse.tutorials.pushnotifications.Application"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.parse.tutorials.pushnotifications.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <service android:name="com.parse.PushService" />
        <receiver android:name="com.parse.ParseBroadcastReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="android.intent.action.USER_PRESENT" />
            </intent-filter>
        </receiver>
        <receiver android:name="com.parse.GcmBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />

                <category android:name="com.parse.tutorials.pushnotifications" />
            </intent-filter>
        </receiver>

        <receiver android:name="com.parse.tutorials.pushnotifications.RetrieveNotify" android:exported="false">
                  <intent-filter>
                    <action android:name="RetrieveNotify" />
                  </intent-filter>
        </receiver>

    </application>

</manifest

>

我想在log.e:

中收到此消息

enter image description here

虽然我收到了你好世界!通知,我没有任何log.e!

2 个答案:

答案 0 :(得分:0)

解析后来的SDK版本的默认行为是推送通知。您可以选择接收推送消息。

答案 1 :(得分:0)

根据您的清单,使用parse webconsole

发送这样的JSON警报
{ "message": "Here goes the promotional text", "messageTitle": "Promotional Title", "action": "RetrieveNotify" }

在您的广播接收器中,

    public void onReceive(final Context context, final Intent intent) {

        action = intent.getAction();

if (action.equals("RetrieveNotify"){
            notificationPayload = new JSONObject(intent.getExtras().getString("com.parse.Data"));

            message = notificationPayload.getString("message");
            messageTitle = notificationPayload.getString("messageTitle");

//记录您的变量     }