通过单击Parse的推送通知打开活动

时间:2014-04-03 15:41:32

标签: android notifications push-notification broadcastreceiver parse-platform

我希望从Parse接收推送通知并打开List活动,并在开始活动之前使用intent.putextra(“dataFromParse”)。 我能够接收推送,但只使用以下命令打开MainActivity:

PushService.setDefaultPushCallback(this, MainActivity.class);
ParseInstallation.getCurrentInstallation().saveInBackground();

我想将此作为默认值,但也应该能够启动List活动。 我也试过使用客户接收器,但是我只能在接收推送时直接打开活动,而不是点击它。

的manifest.xml:

<receiver android:name="com.example.Push android:exported="false">
    <intent-filter>
        <action android:name="com.example.UPDATE_STATUS" />
    </intent-filter>
</receiver>

Push.java:

public class Push extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
      //Start activity
     }
}

我不确定的是我应该如何在后台捕获推送,并说当用户点击通知时它应该使用特定的intent.putExtra(“dataFromParse”)打开List活动。我应该在哪里编码以及如何编码?在MainActivity中,在List活动中,还是与客户接收者做其他事情?

4 个答案:

答案 0 :(得分:13)

解决它,将默认活动作为MainActivity,但检查意图,以及&#34; com.parse.Data&#34;我将开始一个新的意图。

Intent intent = getIntent();
Bundle extras = intent.getExtras();
String jsonData = extras.getString("com.parse.Data");
JSONObject json = new JSONObject(jsonData);
String pushStore = json.getString("data");
if(pushStore!=null) {
    Intent pushIntent = new Intent();
    pushIntent.setClassName(MainActivity.this, "package.name.List");
    pushIntent.putExtra("store", pushStore);
    startActivity(pushIntent);
}           

然后用json发送推送:{&#34; alert&#34; :&#34;测试&#34;,&#34;数据&#34; :&#34; store1&#34; }

@arvindwill希望这会有所帮助。

答案 1 :(得分:2)

试试这个......它完美无缺......

try {
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            String jsonData = extras.getString("com.parse.Data");
            JSONObject json;
            json = new JSONObject(jsonData);
            String pushStore = json.getString("alert");
            data.setText(pushStore);
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

答案 2 :(得分:1)

根据https://www.parse.com/docs/android/guide#push-notifications-customizing-notifications上的官方Parse文档:

  

如果你的推送没有“uri”参数,onPushOpen将调用你的应用程序的启动器活动。

因此,当前接受的答案仅考虑了您的推送没有“uri”并且打开您的MainActivity的情况。如果要打开具体的Activity,可以为该Activity定义intent-filter并在推送时发送该intent-filter uri。

答案 3 :(得分:0)

您应该覆盖接收器类中的onPushOpen(...)方法:

import com.parse.ParsePushBroadcastReceiver;

public class PushIntentReceiver extends ParsePushBroadcastReceiver {

    @Override
    protected void onPushOpen(Context context, Intent intent) {
        JSONObject pushData = null;

        try {
            pushData = new JSONObject(intent.getStringExtra(KEY_PUSH_DATA));            
            Intent pushIntent = new Intent(context, YourActivity.class);        
            pushIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            pushIntent.putExtra("store", pushData.getString("data"));
            context.startActivity(pushIntent);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}