OneSignal Android NotificationOpenedHandler - start activity

后端 未结 2 1276
遇见更好的自我
遇见更好的自我 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: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);
            }
        }
    }
    

提交回复
热议问题