gpt4 book ai didi

java - OneSignal SDK : How to open MainActivity after user taps on notification

转载 作者:行者123 更新时间:2023-12-01 18:04:31 25 4
gpt4 key购买 nike

如果用户点击从 OpenSignal 发送的推送通知,如何打开主 Activity 。我想覆盖应用程序处于 Activity 状态时导致一些问题的默认行为。我根据文档添加了以下行

<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />

现在如果应用程序关闭,我如何打开MainActivity,并让它执行NotificationOpenedHandler。

谢谢。

最佳答案

如果您仍然希望在点击 OneSignal 通知时打开/恢复启动器/主 Activity,请将以下代码添加到您的 Activity 中。

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有关更多详细信息的说明。

如果您需要执行更自定义的操作,请保留上面提到的 list 条目,并将 OneSignal NotificationOpenedHandler 添加到 Application 中的 OneSignal.startInit > 类。

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有关此回调的更多详细信息。

关于java - OneSignal SDK : How to open MainActivity after user taps on notification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37772166/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com