Open activity by clicking on the push notification from Parse

前端 未结 4 1083
抹茶落季
抹茶落季 2021-02-14 06:29

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

相关标签:
4条回答
  • 2021-02-14 06:54

    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.

    0 讨论(0)
  • 2021-02-14 07:09

    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();
            }
        }
    }
    
    0 讨论(0)
  • 2021-02-14 07:11

    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();
        }
    
    0 讨论(0)
  • 2021-02-14 07:15

    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.

    0 讨论(0)
提交回复
热议问题