我通过OneSignal
收到通知,但我想在点击通知时打开该应用
private static class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
@Override
public void notificationOpened(String message, JSONObject additionalData, boolean isActive) {
try {
if (additionalData != null) {
if (additionalData.has("actionSelected"))
Log.d("OneSignalExample", "OneSignal notification button with id " + additionalData.getString("actionSelected") + " pressed");
Log.d("OneSignalExample", "Full additionalData:\n" + additionalData.toString());
}
} catch (Throwable t) {
t.printStackTrace();
}
答案 0 :(得分:4)
在ExampleNotificationOpenedHandler
中添加一个构造函数,该构造函数将上下文作为参数
private Context mContext;
public ExampleNotificationOpenedHandler(Context context) {
mContext = context;
}
使用OneSignal
构造函数的初始ExampleNotificationOpenedHandler
,其中包含应用程序类中的上下文
public void onCreate() {
super.onCreate();
OneSignal.startInit(this)
.setNotificationOpenedHandler((OneSignal.NotificationOpenedHandler)
new ExampleNotificationOpenedHandler(this))
.init();
}
准备意图并使用上下文
开始您的活动@Override
public void notificationOpened(OSNotificationOpenResult result) {
try {
if (additionalData != null) {
Intent intent = new Intent(mContext, DetailsActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("key", <additionalData to be sent>);
mContext.startActivity(intent);
}
} catch (Throwable t) {
t.printStackTrace();
}
答案 1 :(得分:0)
在OneSignalPushApplication类中,初始化:
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.autoPromptLocation(true)
.init();
并将ExampleNotificationOpenedHandler声明为:
private class ExampleNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
// This fires when a notification is opened by tapping on it.
@Override
public void notificationOpened(OSNotificationOpenResult result) {
String title=result.notification.payload.title;
String desc=result.notification.payload.body;
Log.d("xiomi", "Received Title "+title);
Log.d("xiomi", "Received Desc "+desc);
Intent intent = new Intent(getApplicationContext(), YourMainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("push_title", title);
intent.putExtra("push_message", desc);
startActivity(intent);
}
}