android - 在 Android App 中实现 Pubnub 通知

标签 android firebase push-notification firebase-cloud-messaging pubnub

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

compile 'com.pubnub:pubnub-android:3.7.10'

然后是 FCM 通知

> compile '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'

希望这对您有所帮助..让我知道您的反馈。

关于android - 在 Android App 中实现 Pubnub 通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45507240/

相关文章:

android - 动画后按钮单击不起作用

Azure 通知中心 : 'Test Send' was successfully sent,,但没有任何 GCM 流量。为什么?

ios - 推送在 iPhone 6 上不起作用

google-chrome - 有没有办法在 Chrome 或 Firefox 关闭时显示桌面通知?

android - Cordova : "Android SDK: not installed"

android - 设置TableRow的高度

android - Android 应用程序中的 Firebase 云消息传递仅适用于通知而不适用于数据消息

android - 当应用程序在 Firebase 后台运行时如何处理通知

ios - 使用observeSingleEvent更新Firebase本地数据

angularjs - 如何将音频文件上传到 Firebase 存储?