Open activity by clicking on the push notification from Parse

柔情痞子 提交于 2020-01-22 13:52:07

问题


I want to receive a push notification from Parse and open an List activity and use intent.putextra("dataFromParse") before starting the activity. I'm able to receive the push but only open the MainActivity by using this:

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

I want to have this as the default, but should also be able to start the List activity. I have also tried using a customer receiver, but then I'm only able to directly open the activity when receiving the push, not when clicking it.

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
     }
}

The thing I'm not sure about is how I should capture the push in the background and say that it should open the List activity with the specific intent.putExtra("dataFromParse") when the user clicks the notification. Where should I code it and how? In the MainActivity, in the List activity, or do something other with the customer receiver?


回答1:


Solved it, having default activity as MainActivity, but checking the intent, and if there is something in "com.parse.Data" I will start a new intent.

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);
}           

Then just send a push with json: { "alert" : "Test", "data" : "store1" }

@arvindwill Hope this will help.




回答2:


Try this ... it works perfectly....

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();
    }



回答3:


As per official Parse documentation on https://www.parse.com/docs/android/guide#push-notifications-customizing-notifications :

If your push has no "uri" parameter, onPushOpen will invoke your application's launcher activity.

So, the current accepted answer only takes into account the case where your push has no "uri" and it opens your MainActivity. If you want to open a concrete Activity, you can define an intent-filter for that Activity and send that intent-filter uri on your push.




回答4:


You should override onPushOpen(...) method in your receiver class:

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();
        }
    }
}


来源:https://stackoverflow.com/questions/22842571/open-activity-by-clicking-on-the-push-notification-from-parse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!