officebluesource / ChoiceSDK

A SDK that aims to wrap similar functionalities from HMS and GMS services to one common interface.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Notifications in closed app

nicola73 opened this issue · comments

On GMS push notifications are not delivered when the app isn't in execution. On HMS they arrive even when the app is not running. How is it possible to have the same behavior on GMS?

Please refer to the Firebase Cloud Messaging documentation: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages

ChoiceSDK implements the FirebaseMessagingService for you and forwards received messages to MessagingService.getMessageReceivedObservable(). In certain cases Firebase delivers the remote notification directly to the system tray, and you have to extract the notification data from the intent's extras.

Thanks for the reply. The problem is that notifications don't reach the system tray when the app is closed, while on HMS they do. Here is my code:

class NotificationService {
	companion object {
		const val NOTIFICATION_SERVICE_RESULT = 100

		fun start(activity: Activity) {
			val tokenObserver = object : DisposableObserver<String>() {
				override fun onNext(token: String) {
					Settings.FCMToken = token
					...
				}

				override fun onError(e: Throwable?) = activity.createPendingResult(NOTIFICATION_SERVICE_RESULT, Intent().putExtra("error", e), PendingIntent.FLAG_ONE_SHOT).send(AppCompatActivity.RESULT_CANCELED)
				override fun onComplete() {}
			}

			val messageObserver: DisposableObserver<RemoteMessage> = object : DisposableObserver<RemoteMessage>() {
				override fun onNext(remoteMessage: RemoteMessage) = showNotification(remoteMessage)
				override fun onError(e: Throwable?) {}
				override fun onComplete() {}
			}

			val messagingService = MessagingRepositoryFactory.getMessagingService()
			messagingService.getNewTokenObservable().subscribeWith(tokenObserver)
			messagingService.getMessageReceivedObservable().observeOn(AndroidSchedulers.mainThread()).subscribeWith(messageObserver)
			MessagingFactory.getMessaging().subscribeToTopic(BuildConfig.NOTIFICATION_HUB)
		}

		fun showNotification(notification: RemoteMessage) {
		...
		}
...

Update, on API 29 the notification arrives regularly in the tray even if the app is closed.

Actually I don't think there should be a difference between API levels.

Have you tried to send a pure data message? According to the FCM documentation they are not delivered to the system tray and should always be received by ChoiceSDK.

Also, you should not force-close the app, as this could also kill the FCM messaging service until you restart the app (see firebase/quickstart-android#378).

We have done further tests and everything seems to work on different devices. I hope the library is always kept up to date. Thanks for the great work.