[英]Open application on notification click in OneSignal
I am getting notification using this through OneSignal
, but I want to open the application when I click on notification 我正在通过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();
}
Add a constructor in ExampleNotificationOpenedHandler
that takes context as a parameter 在ExampleNotificationOpenedHandler
中添加一个将上下文作为参数的构造函数
private Context mContext;
public ExampleNotificationOpenedHandler(Context context) {
mContext = context;
}
Init OneSignal
with ExampleNotificationOpenedHandler
constructor with context inside application class 使用ExampleNotificationOpenedHandler
构造函数初始化OneSignal
,并在应用程序类中添加上下文
public void onCreate() {
super.onCreate();
OneSignal.startInit(this)
.setNotificationOpenedHandler((OneSignal.NotificationOpenedHandler)
new ExampleNotificationOpenedHandler(this))
.init();
}
Prepare intent and start your activity using context 准备意图并使用上下文开始您的活动
@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();
}
In your OneSignalPushApplication class, initialize: 在您的OneSignalPushApplication类中,初始化:
OneSignal.startInit(this)
.setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
.inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
.autoPromptLocation(true)
.init();
and declare ExampleNotificationOpenedHandler as: 并将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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.