如果用户点击OpenSignal发送的推送通知,我该如何打开主要活动。我想覆盖在App处于活动状态时导致某些问题的默认行为。我按照文档
添加了以下行<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
现在,如果app已关闭,我该如何打开MainActivity,让它执行NotificationOpenedHandler。
谢谢。
答案 0 :(得分:4)
如果您仍然希望在点击OneSignal通知时打开/恢复您的启动器/主活动,请将以下代码添加到您的活动内容中。
private static boolean activityStarted;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ( activityStarted
&& getIntent() != null
&& (getIntent().getFlags() & Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) != 0) {
finish();
return;
}
activityStarted = true;
}
有关详细信息,请参阅Resume last Activity when opening a Notification说明。
如果您需要执行更多自定义操作,请保留上面提到的清单条目,并在NotificationOpenedHandler
课程中向OneSignal.startInit
添加OneSignal Application
。
import com.onesignal.OneSignal;
public class YourAppClass extends Application {
@Override
public void onCreate() {
super.onCreate();
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.init();
}
// This fires when a notification is opened by tapping on it or one is received while the app is running.
private class ExampleNotificationOpenedHandler implements NotificationOpenedHandler {
@Override
public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
// The following can be used to open an Activity of your choice.
/*
Intent intent = new Intent(getApplication(), YourActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
*/
// Follow the instructions in the link below to prevent the launcher Activity from starting.
// https://documentation.onesignal.com/docs/android-notification-customizations#changing-the-open-action-of-a-notification
}
}
有关此回调的详细信息,请参阅4. Add Optional NotificationOpenedHandler。