I want to trigger a specific event (like ordinary vibration) when the device gets an notification from the Firebase Notifications.
All I discovered so far is that one can handle the on_click of a notification that was sent with display-messages in the background of the app.
Is it possible to let the device vibrate in the very moment the notification arrives? I would love to get the users attention to participate on my field study by answering the question sheet in the moment, the notification comes in.
Thanks alot!
There are two types of messages:
data messages (with a data property in the JSON)
notification messages (with only a notification property in the JSON)
If a notification/data message arrives while your app is active, you can handle it in onMessageReceived and do whatever you want.
If a data message arrives while your app is inactive, you can handle it in onMessageReceived and do whatever you want.
To make the phone vibrate in these cases, see the excellent example from Wizard.
If a notification message arrives while your app is inactive, it is automatically handled by the system and you can't control what happens.
Also see the documentation on message types as there are some more nuances.
Is it possible to let the device vibrate in the very moment the
notification arrives?
Yes, you can define it while generating Notification using NotificationManager class -
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true)
....
..
.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 });
Related
I'm sending data payloads through FCM and handling them in the onMessageReceived() method of FirebaseMessagingService. The problem is, when the app is in the background, the push notifications seem to be popping up each time the device handles them. I'd like them to be silent similar to how Android delivers notification payloads.
How do I make sure the notifications do not create this popup effect? I'm on Android 9.
Here's how the notification is generated on the client:
var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot);
var notificationBuilder = new NotificationCompat.Builder(this, myChannelId)
.SetContentTitle(title)
.SetContentText(body)
.SetSmallIcon(Resource.Mipmap.ic_launcher)
.SetAutoCancel(true)
.SetContentIntent(pendingIntent);
and this is how it's notified:
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
notificationBuilder.SetChannelId(myChannelId);
var notificationManager = NotificationManager.FromContext(this);
notificationManager.Notify(myId, notificationBuilder.Build());
Notifications will appear with a popup effect ('Heads Up') view like shown in the screenshot if you set the notification channel priority to IMPORTANCE_HIGH or above, so to stop that happening, you should ensure the channel priority is IMPORTANCE_DEFAULT or lower.
If you have other notifications that you do want to use the heads up view, then you should create a separate NotificationChannel for this notification. Note that notification channels appear as 'Categories' in the notification settings for your app, allowing the user to choose which kinds of notifications they want to see from you, and two notifications wanting a different priority is a good indicator that they should be in different categories
When app is killed / in background...
When only 'data' payload is passed in request, notification will be generated with popup, and handled by user in 'onMessageReceived'
When only 'notification' payload is passed in request, notification will be generated without popup, and handled by android OS
When both 'data' and 'notification' payload are passed in request, notification will be generated without popup, and handled by android OS and data will be available for handling in extras of the intent.
So, in our case (don't want to show notification as popup) pass both 'data' and 'notification' payloads in request params. (Here title and body will be shown, what is passed in notification payload)
Refer official link: https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages
Use android lifecyclehandler and check whether app is in foreground or background and don't show the notification if app is in foreground lifecyclerhandler
I have implemented FCM push notifications in an android app.
While I am logged in to the app. I get the notification in the form that I am expecting as below.
When the app is in background I receive the json response instead as below.
Following is the code I have added in onMessageRecieved()
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle("App")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentIntent(pendingIntent)
.setStyle(new NotificationCompat.BigPictureStyle()
.bigPicture(bitmap))/*Notification with Image*/;
How can I get the notification in the same manner in both cases.
Any help will be appreciated thank you
After setting up FCM in Android application, you can use Firebase console to send notifications. When foregrounded application receives a notification, onMessageReceived method is invoked. You should override this method to handle notification, but the problem is when the application is in the background and receives a notification, notification delivers to the device’s system tray and you can not handle notification with onMessageReceived method. When the user taps on a notification, it opens the app launcher by default. For example consider you want to do a specific task when a notification is received to the user or do something in background without the user realizing or don’t want to show notification dialog to the user, you can’t do these when the application is backgrounded.
Message Types
With FCM you can send two types of messages to clients :
1- Notification message, sometimes thought of a “display message”
2- Data message, which are handled by the client application
According to Google documents a notification message has a 2KB limit and a predefined user-visible keys. Data messages let developers send up to 4KB custom key-value pairs.
Solution:
If you want to handle notification when the application is backgrounded you should send data message and use onMessageReceived method.
Here is the complete article.
That is so because push notifications handles in foreground and background in different ways.
From the documentation
Notification messages delivered when your app is in the background. In
this case, the notification is delivered to the device’s system tray.
A user tap on a notification opens the app launcher by default.
Messages with both notification and data payload, both background and
foreground. In this case, the notification is delivered to the
device’s system tray, and the data payload is delivered in the extras
of the intent of your launcher Activity.
Also you have to remember that there are 2 types of firebase pushes - Notification message and Data message.
Looks like you are using Notification messages, so they are working as expected.
Just use data type of messages and so you can always handle them as you need.
I have a strange issue. I have two way to send notifications in my Android app; one from the Android service and the other through FCM.
The scenarios are as follows:
Regardless of whether the app is running or not, the icon of the notification sent from the Android service appears correctly.
When the app is running, the notification icon appears still appears correctly if I send the notification via FCM.
But if the app isn't running and I send the notification via FCM, a white square is displayed instead of the notification icon.
My code in FCMService:
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Android App")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
Most likely your problem is the difference between notification-messages and data-messages.
Please read: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Use notification messages when you want FCM to handle displaying a
notification on your client app's behalf. Use data messages when you
want to process the messages on your client app.
Currently the FCM Web Console only sends notification-messages
So all the messages sent via Web Console (or via API with a notification payload) will be have in this way:
if the app is closed or in background: FCM will display the notification. if you want to customize it you can, but you need to provide specific configuration (in the manifest or in the send API call) see https://firebase.google.com/docs/cloud-messaging/android/client#manifest
if the app is in foreground: FCM will call onMessageReceived()
.
If the behavior that you want is that onMessageReceived() is always called:
then you need to use a data-only (no notification) message
This is a FMC bug detailed in github fcm page.
https://github.com/firebase/quickstart-android/issues/4
In Android, if a user sets "Show Notifications" of a specific App to off, is the notification still created? If yes, is it somehow possible to access these created but not shown notifications?
As an example, consider that to create a Status bar Notification, below code is used:
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
And to POST this notification, below code is used:
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(mId, mBuilder.build());
But since the "Show Notification" is OFF, above code don't show the Notification on Status bar. So as you can see, although the NotificationCompat.Builder has CREATED a Notification object/instance and filled it with other details such as Title or Text etc. you can NOT access it as it is NOT Allowed to POST by System because of user's preference settings.
Also, Since Android 4.1, users can turn off notifications of an app from application manager, but there is NO API to acheive the same from Application code (on Non-rooted device,that is). And It is not possible to disable notifications from other apps, and you can only control notifications generated by your own app.
Few more pointers that you must have in mind:
1.As a developer we have no way to know whether a call to notify was effective or not. So if I really need to check if the notifications are disabled for the current application there is NO Such setting for that in the API.
2.You really shouldn't concern yourself with it. Just assume your notification was successful. If the user has explicitly disabled your notifications, then he/she probably had good reason to do so, and your application should not care whether the notification was displayed or not
I'm sending a C2DM update to my Android app every 1/2 hour, which creates a Notification. Problem is, when I wake up in the morning I get 15 Notifications queued up in the status bar.
How do I only keep the latest notification, overwriting previous ones?
I tried looking at the C2DM documentation (http://code.google.com/android/c2dm/) which mentions a parameter called collapse_key, but I couldn't find an explanation for how to use it, nor am I sure the solution lies on the C2DM side.
Thanks!
If you want to cancel any previous notifications that has been set on the view you can try setting one of these flags.
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_UPDATE_CURRENT
Something like this should replace your old notification i believe
NotificationManager mManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this,test.class);
Notification notification = new Notification(R.drawable.icon, "Notify", System.currentTimeMillis());
notification.setLatestEventInfo(this,"App Name","Description of the notification",
PendingIntent.getActivity(this.getBaseContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
mManager.notify(0, notification);
Notification has a property called number that shows a little number below the icon (for multiple notification). It lets you use the same Icon for Multiple Notification.
Use the same ID while updating your notification. :) Cheers.
In addition to the other answers, there is a parameter in your C2DM request that is called delay_while_idle. Make sure you are NOT including that or make it false. Your phone is "idle" when the screen is off (ie while you are sleeping). Google queues up all your messages on the server until the phone is not idle (ie when you turn on the screen in the morning). Then, Google sends all 15 messages at once and you display them at that time.
In the chrome to phone source, there is a method called sendNoRetry with this line:
if (delayWhileIdle) {
postDataBuilder.append("&")
.append(PARAM_DELAY_WHILE_IDLE).append("=1");
}
Make sure it is not true, then Google servers will send you your C2DM message every 30 minutes as expected.
collapse_id key should do the job. For updating any previous notification, just use the same key. To generate a new notification on device, use a different key.
For example,
* for chat notifications use the key "chat" (collapse_id = "chat")
* for invitations use the key "invite" (collapse_id = "invite")
So all the unqiue collapse_id notifications will group on device.
For more details visit: https://documentation.onesignal.com/reference#create-notification