Parse Push - How to Automatically Open an activity without user action on receiving a push on Android

后端 未结 2 794
耶瑟儿~
耶瑟儿~ 2021-02-10 13:53

I have a requirement (android) where my app should run its main activity automatically when a push notification is received without the user clicking on notification in system t

2条回答
  •  梦毁少年i
    2021-02-10 14:20

    Yes, it's possible. Parse.com documentation says:

    You can also specify an Intent to be fired in the background when the push notification is received. This will allow your app to perform custom handling for the notification, and can be used whether or not you have chosen to display a system tray message. To implement custom notification handling, set the Action entry in your push notification data dictionary to the Intent action which you want to fire. Android guidelines suggest that you prefix the action with your package name to avoid namespace collisions with other running apps.

    So you send push notifications in this way:

    JSONObject data = new JSONObject("{\"action\": \"com.example.UPDATE_STATUS\""}));
    
    ParsePush push = new ParsePush();
    push.setData(data);
    push.sendPushInBackground();
    

    Then in your AndroidManifest.xml register a broadcast receiver that will be called whenever a push notification is received with an action parameter of com.example.UPDATE_STATUS:

    
      
        
      
    
    

    In your broadcast receiver you can start a new activity:

    public class MyBroadcastReceiver extends BroadcastReceiver {
    
      @Override
      public void onReceive(Context context, Intent intent) {
        context.startActivity(new Intent(context, MyActivity.class));
      }
    }
    

提交回复
热议问题