如何使用parse.com在Android设备上接收解析推送notifiactions

时间:2014-01-30 10:47:56

标签: android android-intent parse-platform

如何从解析推送通知中获取意图。如果任何人实施解析推送请帮助。

Parse.initialize(Splash.this,"id","id");
ParseInstallation.getCurrentInstallation().saveInBackground();
PushService.setDefaultPushCallback(Splash.this, ParsePush.class);

像这样实施。

无法在jsonData中获取任何值。

public class ParsePush extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        ParseInstallation.getCurrentInstallation().saveInBackground();
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        String jsonData = extras.getString("com.parse.Data");
        System.out.println("Data Json : " + jsonData);
    }

}

需要从推送通知(解析)实现一个意图。 需要显示点击推送的活动..请帮助。

4 个答案:

答案 0 :(得分:5)

您必须编写解析广播接收器才能接收通知。 写在你的清单

  <receiver
        android:name="com.example.package.ParseBroadcastReceiver"
        android:exported="false"
        android:enabled="true">
        <intent-filter>
            <action android:name="com.example.package.MESSAGE" />
        </intent-filter>
    </receiver>

然后定义广播接收器

public class ParseBroadcastReceiver extends BroadcastReceiver{

public static final String ACTION                       =   "com.example.package.MESSAGE";
public static final String PARSE_EXTRA_DATA_KEY         =   "com.parse.Data";
public static final String PARSE_JSON_CHANNEL_KEY       =   "com.parse.Channel";

@Override
public void onReceive(Context context, Intent intent) {

     String action = intent.getAction();
        String channel = intent.getExtras().getString(PARSE_JSON_CHANNEL_KEY);
        JSONObject json = new JSONObject(intent.getExtras().getString(PARSE_EXTRA_DATA_KEY));

    }

现在这个json对象将包含你从parse发送的数据。 例如,如果您必须使用javascript api发送通知

   Parse.Push.send({where: query, // Set our Installation query
                data: {                   
                    triggerKey:triggerValue, 
                    objectType:"android",
                    action:"com.example.package.MESSAGE"
                }
              },{
              success: function() {
                // Push was successful

              },
              error: function(error) {
                // Handle error

              }
            });

请注意,在推送通知中,您必须提及“操作”键,它应与您在广播接收器意图过滤器中提到的相同。

答案 1 :(得分:5)

像这样使用:

  public class Application extends android.app.Application {

  public Application() {
  }

  @Override
  public void onCreate() {
    super.onCreate();
    Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY");

    PushService.setDefaultPushCallback(this, MainActivity.class);
  }
}

// MainActivity.java - 单击通知时需要打开的活动。

在您的清单文件中添加此应用程序。

<application    android:name="com.parse.tutorials.pushnotifications.Application"
        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> </application>

并在MainActivity类中添加此行,您需要在单击通知时打开该行。

Bundle mBundle = getIntent().getExtras();
if (mBundle != null) {
    String mData = mBundle.getString("com.parse.Data");
    System.out.println("DATA : xxxxx : " + mData);
}

答案 2 :(得分:0)

在您的活动中尝试以下代码:

    Parse.initialize(this, Constants.PARSE_APPLICATION_ID,
            Constants.PARSE_CLIENT_ID);


    ParseUser.enableAutomaticUser();
    ParseACL defaultACL = new ParseACL();

    // If you would like all objects to be private by default, remove this line.
    defaultACL.setPublicReadAccess(true);

    ParseACL.setDefaultACL(defaultACL, true);
    // To track statistics around application
    ParseAnalytics.trackAppOpened(getIntent());

    // inform the Parse Cloud that it is ready for notifications
    PushService.setDefaultPushCallback(this, TestActivity.class);
    try {
        Bundle b = getIntent().getExtras(); 
        JSONObject jsonObject = new JSONObject(b.getString("com.parse.Data"));
        Toast.makeText(getApplicationContext(), "" + jsonObject.getString("alert"), Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

答案 3 :(得分:0)

您可以从here

下载完整代码

依赖添加Google Play服务库

在应用程序类中初始化Parse

   Parse.initialize(this, getResources().getString(R.string.applicationid), getResources().getString(R.string.clientkey));


    ParsePush.subscribeInBackground("", new SaveCallback() {
        @Override
        public void done(ParseException e) {
            if (e == null) {

                Log.d("com.parse.push", "successfully subscribed to the broadcast channel.");
            } else {
                Log.e("com.parse.push", "failed to subscribe for push", e);
            }
        }
    });

在AndroidMainifest.xml中添加以下权限

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

<!--Change with your package name-->
<permission android:name="com.nkdroid.android.pushnotification.permission.C2D_MESSAGE" android:protectionLevel="signature" />
<!--Change with your package name-->
<uses-permission android:name="com.nkdroid.android.pushnotification.permission.C2D_MESSAGE" />

还添加以下内容

    <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" />

            <!--Change with your package name-->
            <category android:name="com.nkdroid.android.pushnotification" />
        </intent-filter>
    </receiver>
    <receiver android:name="com.parse.ParsePushBroadcastReceiver" android:exported="false">
        <intent-filter>
            <action android:name="com.parse.push.intent.RECEIVE" />
            <action android:name="com.parse.push.intent.DELETE" />
            <action android:name="com.parse.push.intent.OPEN" />
        </intent-filter>
    </receiver>

    <!-- add project number of google console project-->
    <meta-data android:name="com.parse.push.gcm_sender_id" android:value="@string/sender_id" />

    <!-- replace icon with your push icon identifier -->
    <meta-data android:name="com.parse.push.notification_icon" android:resource="@mipmap/ic_launcher" />