Implementing Pubnub notification in Android App

时间:2017-08-04 12:46:40

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

I am developing chat app using Pubnub but I am having a problem with implementing push notification. I have used following library for Pubnub

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

and following for the FCM notification

> compile 'com.google.firebase:firebase-messaging:9.6.0'

I am able to get push message if I send a message manually from Firebase console that indicates FCM has integrated properly.

I have enabled add-on as well for Push Notification in Pubnub account dashboard.

Then added FCM server as well over there.

Features like real-time messaging, History API callback and Presence API working pretty fine.

I have stuck in implementing push notification only.

When I searched for that I came to know about this method

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.
        }
    });

but above method is no longer accessible for the SDK version that I've been using.

I know below method for the same but have no idea how it works.

 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);

        }
    });

Any help or assistance in this matter will be greatly appreciated..!!

Thanks in advance.

1 个答案:

答案 0 :(得分:1)

Your code is right, just send the message in this format:

 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;
    }

And use this library:

compile 'com.pubnub:pubnub:4.0.9'

Hope this will help you out..let me know your feedback.