OneSignal Android NotificationOpenedHandler - start activity

后端 未结 2 1277
遇见更好的自我
遇见更好的自我 2021-01-05 01:35

Trying to migrate from Parse to OneSignal and I am stuck on how to start a new Activity after the user clicks on a push notification. My handler is working, the log shows th

相关标签:
2条回答
  • 2021-01-05 02:05

    Here's how I made it work:

    public class MyApplication extends Application {
        @Override
        public void onCreate() {
            super.onCreate();
            OneSignal.startInit(this)
                    .setNotificationOpenedHandler(new MyNotificationOpenedHandler(this))
                    .init();
        }
    }
    

    The NotificationOpenedHandler custom class

    public class MyNotificationOpenedHandler implements OneSignal.NotificationOpenedHandler {
    
        private Application application;
    
        public MyNotificationOpenedHandler(Application application) {
            this.application = application;
        }
    
        @Override
        public void notificationOpened(OSNotificationOpenResult result) {
    
            // Get custom datas from notification
            JSONObject data = result.notification.payload.additionalData;
            if (data != null) {
                String myCustomData = data.optString("key", null);
            }
    
            // React to button pressed
            OSNotificationAction.ActionType actionType = result.action.type;
            if (actionType == OSNotificationAction.ActionType.ActionTaken)
                Log.i("OneSignalExample", "Button pressed with id: " + result.action.actionID);
    
            // Launch new activity using Application object
            startApp();
        }
    
        private void startApp() {
            Intent intent = new Intent(application, MyActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
            application.startActivity(intent);
        }
    }
    

    Don't forget to add this to your manifest:

    <application ...>
       <meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" />
    </application>
    
    0 讨论(0)
  • 2021-01-05 02:30

    The short answer to this issue is to include your handler for the push notification open within the same class where you initialize OneSignal:

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            // Init OneSignal
            OneSignal.startInit(this).setNotificationOpenedHandler(new NotificationOpenHandler()).init();
    
            Toolbar mToolbar = (Toolbar) findViewById(R.id.app_bar);
            setSupportActionBar(mToolbar);
            getSupportActionBar().setDisplayUseLogoEnabled(true);
            getSupportActionBar().setLogo(R.drawable.ic_launcher);
            getSupportActionBar().setDisplayShowTitleEnabled(false);
            if (savedInstanceState == null) {
                getFragmentManager().beginTransaction()
                        .add(R.id.container, new RecordingsFragment())
                        .commit();
            }
        }
    
        class NotificationOpenHandler implements OneSignal.NotificationOpenedHandler {
            // This fires when a notification is opened by tapping on it.
            @Override
            public void notificationOpened(OSNotificationOpenResult result) {
                OSNotificationAction.ActionType actionType = result.action.type;
                JSONObject data = result.notification.payload.additionalData;
    
                String stationName = data.optString("stationName");
                String timestamp = data.optString("timestamp");
                String filename = data.optString("filename");
                String url = getString(R.string.callResourceUrl) + filename;
    
                Intent intent = new Intent(getApplicationContext(), CallActivity.class);
                intent.putExtra("stationName", stationName);
                intent.putExtra("time", timestamp);
                intent.putExtra("url", url);
    //            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题