FCM Unity plugin gives you message received callback only when your app is in foreground because that is when you can register fro callback.
I am developing a plugin which shows notification, is there any way where I can get message data when app is in background to show push notifications?
As we know Unity engine is a foreground process, so we can not expect Unity to run scripts while the app is closed.
But firebase can itself run in the background regardless of app's engine state.
It will receive the message and save it in the intent of the activity, so when the user opens the app the activity will run and the intent data will be passed to onMessageReceived.
According to documentation, to achieve this, you need to use com.google.firebase.MessagingUnityPlayerActivity instead of UnityPlayerActivity.
Or if you cannot do this and are extending some other activity neighther firebase's, check this part of the docs.
Related
I have noticed that when I send a push notification (Firebase Messaging Service) to my device my Application object is created. This is without clicking on the notification. Simply the act of viewing the notification creates the application. Further, it also starts the Jetpack AppStartup library. I want to be able to use AppStartup and application create. But I don't want to launch that code when a push notification occurs.
Why does Android do this? Is this part of all android notification, or is it a feature of the third-party push notification sdk I am using? And is there a way in Application.create and AppStartup to distinguish a normal app launch from a push notification triggered launch?
Again, I'm not talking about the user clicking on the notification (and launching the app because of a deeplink). I'm talking about just looking at the notification in the notification dropdown.
Why does Android do this?
Android is starting your app process to run code in your app. Creating an Application instance and calling onCreate() will be part of that, as will creating any ContentProvider objects. IIRC, Jetpack Startup uses a ContentProvider to get control early in your process, though I am not 100% certain of that.
The reason why Android is starting your app process is because your app is causing the Notification to be displayed — specifically, Firebase Cloud Messaging is doing that. If I remember the protocol correctly, Play Services is sending a broadcast Intent that Firebase Cloud Messaging in your app will respond to, and part of that code will be displaying the Notification.
And is there a way in Application.create and AppStartup to distinguish a normal app launch from a push notification triggered launch?
onCreate() of an Application subclass has no means of knowing what specifically caused the process to be created, as there can be many possible reasons. If by "AppStartup" you mean Jetpack Startup, I do not recall it having any options here, but I have not spent much time with its API.
I am investigating about React Native and Firebase Cloud Messaging. In the document, I see that has 2 types of message can be send to client is:
Notification message ( Or Notification and data message )
Data - only message
The first one can be trigger by client using onNotification, and it only trigger if the app on foreground, if in background no way to trigger that has a notification. Just onNotificationOpened can be fired.
The second is inverse, the app can trigger on foreground and background. In case the app has been closed/swiped, on Android luckily it can be triggered also by using a Handler background. But on iOS if the app closed/swiped that no way until we open app again.
The problem is what type of 2 message above that I can use to listen the notification coming, and how it can work if app in foreground, background and closed.
Hope someone can help me about this.
Firebase Push notification for Android App, starts the app when received the message while App is not running (killed), even without clicking at the notification. Does any one knows if this is the default Android behavior or there is some settings I need to change?
I'm still not clear on what you mean by open up the app (start).
Here is what I see when a notification-only message is sent to an app that is not in memory (version 11.8.0):
An instance of the app is created. This occurs even when the app does not define a service that extends FirebaseMessagingService.
If the app does define a FirebaseMessagingService, the service is created and destroyed. onMessageReceived() is not called.
In both cases, all processing occurs in the background; no activity is started until the user taps on the notification.
These observations are based on log messages output from my messaging service and Application instance.
While the descriptions in the documentation, such as FCM automatically displays the message to end-user devices on behalf of the client app, correctly indicate that the handling of the message is done "by the system", they don't mention that creation of an app instance is a side effect of that processing.
I have activated push notifications in my application and I have done all the settings with Google and Apple to be able to send from the Appcelerator Dashboard.
I can receive notifications on both Android and iOS without any problems.
In iOS, when i click the notification from the notification center, the application opens and the callback function of Ti.Network.registerForPushNotifications is executed automatically.
The problem is that in Android, the function assigned to the callback event of the cloudPush module is not executed, I understand that it will be because when opening the application the listener is not yet created.
If I receive the notification when the application is open the callback function runs smoothly.
Reading in documentation of Android seems to me to understand that the notification is included in the extras of the intent when opening the application, but I am not clear what extra is, since in Appcelerator I do not find any function to obtain all the extras.
Someone could give me an idea of how to get the payload, whether or not when I click the notification the app is in the background, as if it is totally closed?
Thanks in advance.
I am using fcm push notification for my android app. I was able to display push notification on system tray when app is not launched. When I tap on the notification it opens the app launcher by default and I start an activity A from there. But the issue is, if I put the app to background and click on the app icon it again opens the app launcher rather than opening existing Activity A.
If the app process is killed, start the launcher activity. If the app is in the background, you can pass an intent to the notification which starts a DummyActivity that has no code on it, and immediatelly calls finish() on its onCreate() method. This will bring your app to the foreground.
Several things are not clear in your question. For example: How you send messages (from developer console or through rest api post requests to firebase backend)? What is your desired behaviour for app when push messages come? I will try to give you general answer that probably helps you to address issue and understand how to implement desired behaviour.
In any case, there are two types of Firebase push messages:
data messages
notification messages
more details about it check on Notification & data messages page
If you want to send additional details to activity that you are starting (something similar to bundle extras), you should use data messages and handle those in your service that extends FirebaseMessagingService by overriding onMessageReceived(RemoteMessage remoteMessage) method. This method is preferable for me because it is much more flexible. You can define all the details about showing notification based on received firebase message, including if notifications are bundled, what happens in details when user click notification and almost everything related to it.
If you don't need to start certain activity with some parameters, than you can use push messages and just define click_action. This method allows you to add define title, text and sound of notification (beside some other details) but it is not as flexible as if you send data messages
Here you can find detailed overview of possible parameters that you can use for different type of messages
Hope this helps