使用OneSignal将带有操作按钮的通知从一个设备发送给所有用户

时间:2017-11-16 09:26:48

标签: java android json onesignal

我正在尝试将带有操作按钮的通知从一个设备发送到我的Android应用程序的所有订阅者

这是我的代码

OneSignal.startInit(this)
            .inFocusDisplaying(OneSignal.OSInFocusDisplayOption.Notification)
            .unsubscribeWhenNotificationsAreDisabled(true)
            .init();

    Button onSendNotification2 = (Button)(findViewById(R.id.send_notification_button2));
    onSendNotification2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            OSPermissionSubscriptionState status = OneSignal.getPermissionSubscriptionState();


                try {
                    OneSignal.postNotification(new JSONObject("{'contents': {'en':'Tag substitution value for key1 = {{key1}}'}, " +
                                     "'app_id':{ '61b94305-7527-4681-a257-c3851af3ab82'},"+
                                     "'included_segments':{ ['All']},"+
                                    "'headings': {'en': 'Tag sub Title HI {{user_name}}'}, " +
                                    "'data': {'openURL': 'https://imgur.com'}," +
                                    "'buttons':[{'id': 'id1', 'text': 'Go to GreenActivity'}, {'id':'id2', 'text': 'Go to MainActivity'}]}"),
                            new OneSignal.PostNotificationResponseHandler() {
                                @Override
                                public void onSuccess(JSONObject response) {

                                    Toast.makeText(getApplicationContext() , " Success :  "+ response.toString(), Toast.LENGTH_LONG).show();
                                }

                                @Override
                                public void onFailure(JSONObject response) {
                                    Toast.makeText(getApplicationContext() , " failure :  "+ response.toString(), Toast.LENGTH_LONG).show();
                                }
                            });
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext() , " exception :  "+e , Toast.LENGTH_LONG).show();
                }

        }
    });

运行此代码时出现异常, JSONObject代码中出现问题,我不知道如何正确编写代码。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我在Github的OneSignal示例中解决了我的问题https://github.com/OneSignal/OneSignal-Android-SDK