0

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

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

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

谢谢你。

4

1 回答 1

4

如果您仍然希望在点击 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;
}

有关更多详细信息,请参阅打开通知说明时恢复上一个活动。

如果您需要做一些更自定义的事情,请保留上面提到的清单条目,并在您的课程NotificationOpenedHandler中添加 OneSignal 。OneSignal.startInitApplication

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. 添加可选 NotificationOpenedHandler

于 2016-06-13T20:50:47.910 回答