如何使用OneSignal.com处理Android中的通知点击事件?

时间:2017-03-09 15:13:52

标签: android push-notification notifications handle onesignal

我是android的新手。我想在通知点击时打开一个新活动,现在它会自动打开mainActivity。代码如下。

MainActivity

public class MainActivity extends AppCompatActivity {

    TextView titletv,messagetv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        OneSignal.startInit(this).setNotificationOpenedHandler(new ExampleNotificationOpenedHandler()).init();

        setContentView(R.layout.activity_main);

        titletv=(TextView)findViewById(R.id.title);
        messagetv=(TextView)findViewById(R.id.message);
    }


    // This fires when a notification is opened by tapping on it or one is received while the app is running.
    private 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());
                    messagetv.setText(additionalData.toString());
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }
}

清单

0 个答案:

没有答案