我正在使用Pubnub开发聊天应用程序,但是在实现推送通知时遇到了问题。
我已经将以下库用于Pubnub


  编译'com.pubnub:pubnub-android:3.7.10'


和以下的FCM通知


  >编译'com.google.firebase:firebase-messaging:9.6.0'


如果我从Firebase控制台手动发送一条消息,表明FCM已正确集成,那么我将获得推送消息。

我已经在Pubnub帐户信息中心中为推送通知启用了加载项。

然后在那边也添加FCM服务器。

实时消息传递,History API回调和Presence API等功能都可以正常工作。

我只停留在实现推送通知。

当我搜索时,我才知道这种方法

pubnub.addPushNotificationsOnChannels()
    .pushType(PNPushType.GCM)
    .channels(Arrays.asList("ch1", "ch2", "ch3"))
    .deviceId("googleDevice")
    .async(new PNCallback<PNPushAddChannelResult>() {
        @Override
        public void onResponse(PNPushAddChannelResult result, PNStatus status) {
            // handle response.
        }
    });


但以上方法对于我一直在使用的SDK版本不再可用。

我知道下面的方法相同,但不知道它如何工作。

 mPubNub.enablePushNotificationsOnChannel(channel, firebaseRegId, new Callback() {
        @Override
        public void successCallback(String chanel, Object response) {
            super.successCallback(chanel, response);
            Log.e(TAG, "enablePushNotificationsOnChannel successCallback: " + chanel);
            Log.e(TAG, "enablePushNotificationsOnChannel successCallback: " + response);

            sendNotification();
        }

        @Override
        public void errorCallback(String s, PubnubError pubnubError) {
            super.errorCallback(s, pubnubError);
            Log.e(TAG, "enablePushNotificationsOnChannel errorCallback: " + s);
            Log.e(TAG, "enablePushNotificationsOnChannel errorCallback: " + pubnubError);

        }
    });


在这方面的任何帮助或协助将不胜感激..!

提前致谢。

最佳答案

您的代码正确,只需以以下格式发送消息:

 public Map<String, Object> createmessage(String messageType, String messageId) {
        obj = new JSONObject();
        try {
            obj.put("messageType", messageType);
            obj.put("senderID", SharedPref.getInstance().getInt(SharedConstants.USER_ID) + "");
            obj.put("content", messagestring);
            obj.put("type", contentType);
            obj.put("userName", data.getName());
            obj.put("messageId", messageId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        byte[] encodeddata1 = Base64.encode(obj.toString().getBytes(), Base64.NO_WRAP);
        String data = new String(encodeddata1);

        Map<String, Object> messagepayload = new HashMap<>();
        messagepayload.put("message", notification().toString());
        Map<String, Object> datapayload = new HashMap<>();
        datapayload.put("data", messagepayload);
        Map<String, Object> mobilePayload = new HashMap<>();

        mobilePayload.put("pn_gcm", datapayload);
        mobilePayload.put("pn_other", data);
        mobilePayload.put("pn_debug", true);
        Log.e("published message", mobilePayload.toString());
        return mobilePayload;
    }


并使用此库:

compile 'com.pubnub:pubnub:4.0.9'


希望这可以帮助您。让我知道您的反馈。

10-04 23:10
查看更多