简体   繁体   English

通知打开应用程序,单击OneSignal

[英]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.

相关问题 OneSignal 推送通知 点击打开活动 - OneSignal Push Notification Click to open activity 单击时打开某些活动的 OneSignal 通知 - OneSignal notification to open certain activity on click Unity OneSignal SDK:应用程序未打开,然后我单击推送通知 - Unity OneSignal SDK : Application not opening then I click on push notification Firebase通知,通知单击时无法打开应用程序 - Firebase notification, application wont open on notification click 如何使用 Onesignal 在 android 中打开应用程序时显示自定义通知对话框 - How to show custom notification dialog when application is open in android using Onesignal Android OneSignal,从通知中打开特定片段 - Android OneSignal , open specific fragment from notification 通知点击已打开的应用程序无法在android中重新打开 - Notification click already open application not reopen in android 点击通知栏并在应用终止时打开通知活动(onesignal) - tap notification bar and open notification activity when app kill (onesignal) 当您收到带有单信号的推送通知时,将自动打开应用程序 - Open the app automatically when you receive a push notification with onesignal 尝试打开时,Android Grouped Notification(来自OneSignal)使应用程序崩溃 - Android Grouped Notification (from OneSignal) crashes app when trying to open
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM