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

 ̄綄美尐妖づ 提交于 2019-12-03 08:36:59
makovkastar

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:

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

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

warning to @makovkastar, major changes since version V1.8. for instance: no more com.example.UPDATE_STATUS.

push notifications guide is more clear now: https://www.parse.com/tutorials/android-push-notifications

this is the ParsePushBroadcastReceiver subclass: https://gist.github.com/panchicore/97d5ad25842258576109 this answer have a good tutorial to send/receive local broadcasts: https://stackoverflow.com/a/8875292/155293

Basically onPushReceive will be called when a push is received in the device, in this method use LocalBroadcastManager to make something in the app, for instance, add a new message to a chatroom.

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