这是我如何使它工作:
public class MyApplication extends Application { @Override public void onCreate() { super.onCreate(); OneSignal.startInit(this) .setNotificationOpenedHandler(new MyNotificationOpenedHandler(this)) .init(); } }
在NotificationOpenedHandler
自定义类
public class MyNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler { private Application application; public MyNotificationOpenedHandler(Application application) { this.application = application; } @Override public void notificationOpened(OSNotificationOpenResult result) { // Get custom datas from notification JSONObject data = result.notification.payload.additionalData; if (data != null) { String myCustomData = data.optString("key", null); } // React to button pressed OSNotificationAction.ActionType actionType = result.action.type; if (actionType == OSNotificationAction.ActionType.ActionTaken) Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID); // Launch new activity using Application object startApp(); } private void startApp() { Intent intent = new Intent(application, MyActivity.class) .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); application.startActivity(intent); } }
不要忘记将其添加到清单中: