Open application on notification click in OneSignal

让人想犯罪 __ 提交于 2019-12-23 03:57:08

问题


I am getting notification using this through OneSignal, but I want to open the application when I click on notification

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();
        }

回答1:


Add a constructor in ExampleNotificationOpenedHandler that takes context as a parameter

private Context mContext;

public ExampleNotificationOpenedHandler(Context context) {
    mContext = context;
}

Init OneSignal with ExampleNotificationOpenedHandler constructor with context inside application class

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();
}



回答2:


In your OneSignalPushApplication class, initialize:

OneSignal.startInit(this)
          .setNotificationOpenedHandler(new ExampleNotificationOpenedHandler())
          .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
          .autoPromptLocation(true)
          .init();

and declare ExampleNotificationOpenedHandler as:

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);          

      }
   }


来源:https://stackoverflow.com/questions/42997419/open-application-on-notification-click-in-onesignal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!