【问题标题】:OneSignal SDK: How to open MainActivity after user taps on notificationOneSignal SDK:用户点击通知后如何打开 MainActivity
【发布时间】:2016-06-12 08:28:17
【问题描述】:

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

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

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

谢谢。

【问题讨论】:

    标签: java android push-notification push onesignal


    【解决方案1】:

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

    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 说明。

    如果您需要做一些更自定义的事情,请保留上面提到的清单条目,并在您的 Application 类中添加 OneSignal NotificationOpenedHandlerOneSignal.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

    【讨论】:

      猜你喜欢
      • 2016-05-08
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多