androidfirebasepush-notificationquickblox

quickblox push notification integration in android


I am doing quickblox integration in my app. I have successfully integrated chat functionality and it's working great. But push notification functionality is not working. I have followed the below step to integrate push notifications.

  1. create a project on firebase. Implement cloud messaging and integrate google JSON in the main project.

  2. Login in quickblox account adds the google server key in the push notification.

  3. follow https://docs.quickblox.com/docs/android-push-notifications document and after adding firebase cloude messaging i add below in menifest file.

    <meta-data android:name="com.quickblox.messages.TYPE" android:value="FCM" /> <meta-data android:name="com.quickblox.messages.SENDER_ID" android:value="@string/sender_id" /> <meta-data android:name="com.quickblox.messages.QB_ENVIRONMENT" android:value="DEVELOPMENT" />

and register below services in menifest

`<service android:name="com.quickblox.messages.services.fcm.QBFcmPushListenerService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

<service android:name="com.quickblox.messages.services.fcm.QBFcmPushInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
    </intent-filter>
</service>`
  1. I added automatic push notification and implement it below code in chatActivity

`QBSettings.getInstance().setEnablePushNotification(false); // By default is true

        boolean isEnabled = QBSettings.getInstance().isEnablePushNotification();`
  1. create a subscription for push notification

    QBSubscription subscription = new QBSubscription(QBNotificationChannel.GCM); subscription.setEnvironment(QBEnvironment.DEVELOPMENT); // String deviceId = ""; final TelephonyManager mTelephony = (TelephonyManager) BottomActivity.this.getSystemService( Context.TELEPHONY_SERVICE);

            deviceId = Settings.Secure.getString(BottomActivity.this.getContentResolver(),
                    Settings.Secure.ANDROID_ID);
         subscription.setDeviceUdid(deviceId);
         subscription.setRegistrationID(registrationID);
         subscription.setEnvironment(QBEnvironment.DEVELOPMENT);
         QBPushNotifications.createSubscription(subscription);
    
  2. register a receiver for push notification.

    LocalBroadcastManager.getInstance(this).registerReceiver(pushBroadcastReceiver, new IntentFilter(Consts.ACTION_NEW_FCM_EVENT));

  3. send push message.

    ` String outMessage = messageEditText.getText().toString().trim(); QBEvent qbEvent = new QBEvent(); qbEvent.setNotificationType(QBNotificationType.PUSH); qbEvent.setEnvironment(QBEnvironment.DEVELOPMENT);

                 qbEvent.setMessage(outMessage);
                 qbEvent.setPushType(QBPushType.GCM);
    
                 StringifyArrayList<Integer> userIds = new StringifyArrayList<>();
                 userIds.add(QBSessionManager.getInstance().getSessionParameters().getUserId());
    
                 userIds.add(qbChatDialog.getRecipientId());
                 Log.d(TAG, "My Id: " + qbChatDialog.getRecipientId());
                 qbEvent.setUserIds(userIds);
    
                 QBPushNotifications.createEvent(qbEvent).performAsync(new QBEntityCallback<QBEvent>() {
                     @Override
                     public void onSuccess(QBEvent qbEvent, Bundle bundle) {
    
                     }
    
                     @Override
                     public void onError(QBResponseException e) {
    
                         KeyboardUtils.hideKeyboard(messageEditText);
                         invalidateOptionsMenu();
                     }
                 });
    

    `

After following all steps in result I am able to successfully send a message. I can see a message in quickblox admin pannel it shows delivered message. subscription is also created. But I do not receive a notification on my other phone. But when I am trying with firebase cloud to send a force message then I am able to receive a message typed on firebase.

can anyone guide what I am missing in this? I am new to quicblox.


Solution

  • It seems that you already can receive the push but your BroadcastReceiver (extended from QBFcmPushListenerService) didn't do anything to show the notification for the user. Please check how you make your receiver. I have an example for you: https://github.com/QuickBlox/quickblox-android-sdk/blob/master/sample-chat-java/app/src/main/java/com/quickblox/sample/chat/java/fcm/PushListenerService.java

    Please take a look at this feature:

    public class PushListenerService extends QBFcmPushListenerService {
        private static final String TAG = PushListenerService.class.getSimpleName();
        private static final int NOTIFICATION_ID = 1;
    
        protected void showNotification(String message) {
            NotificationUtils.showNotification(App.getInstance(), SplashActivity.class,
                    App.getInstance().getString(R.string.notification_title), message,
                    R.drawable.ic_logo_vector, NOTIFICATION_ID);
        }
    
        @Override
        public void onMessageReceived(RemoteMessage remoteMessage) {
            super.onMessageReceived(remoteMessage);
        }
    
        @Override
        protected void sendPushMessage(Map data, String from, String message) {
            super.sendPushMessage(data, from, message);
            Log.v(TAG, "From: " + from);
            Log.v(TAG, "Message: " + message);
            if (ActivityLifecycle.getInstance().isBackground()) {
                showNotification(message);
            }
        }
    }
    

    And callback "sendPushMessage" there - it is about receiving the Push Message. This is not a sending. It's like sending it to the user :)

    Please pay attention for:

    if (ActivityLifecycle.getInstance().isBackground()) {
       showNotification(message);
    }
    

    This part of logic depends on application behavior. If the application is running now and in the foreground - Notification will not be shown. If you remove this condition - all caught pushes will be shown.

    P.S. And another suggestion is - Permission for showing notifications by your application was disabled in the system, please check this one if all code above you already implemented.