I'm developing a chat application using Firebase Database in Android.
I've already done the core (chat and user's list activities), but I have yet to do the notification system.
I wanted to implement Google FCM as I am using firebase, but I find an hole in it, as it seems that I can not handle at all notifications delivered to iOs apps that are not in foreground.
As found in the Data notification documentation
On iOS, FCM stores the message and delivers it only when the app is in the foreground and has established a FCM connection. On Android, a client app receives a data message in onMessageReceived() and can handle the key-value pairs accordingly.
And I need to catch the data notification even when the app is in background, I need that specifically because I want to update my badge counter on the app icon to let the user know how many unread messages he has.
I'm now tryng the OneSignal solution can receive notification even when in background, it's free and interfaces with GCM. I'm sad to not stay with Google but if I can't update the badge count using FCM I have to look to an other side.
Any consideration will be appreciated.
Use below code for get badge from server when app is in background or not running because your FirebaseMessagingService is running.
If you have any doubts regarding show badge on app icon than comment it because just yesterday i did show badge on app icon.
public class Custom_FirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "FirebaseMsgService";
//private Activity strPushClickDesti;
String activityName;
#Override
public void zzm(Intent intent) {
Log.i("uniqbadge", "zzm");
Set<String> keys = intent.getExtras().keySet();
for (String key : keys) {
try {
Log.i("uniq", " " + key + " " + intent.getExtras().get(key));
if (key.equals("badge")) {
String cnt = intent.getExtras().get(key).toString();
int badgeCount = Integer.valueOf(cnt);
Log.i("uniq", " badge count " + badgeCount);
ShortcutBadger.applyCount(this, badgeCount);
Log.i("uniq", " " + "end");
}
} catch (Exception e) {
Log.i("uniqbadge", "zzm Custom_FirebaseMessagingService" + e.getMessage());
}
}
super.zzm(intent);
}
There is in fact a feature explored by OneSignal to achieve this behavior. It is the content-available value in the notification payload. The good news is that is native to iOS 7 and above. So if you can customize the payload in FCM, it is possible. Just activate remote-notification in background modes and handle the badge update in application(didReceiveRemoteNotification:fetchCompletionHandler:) and you are good to go.
Related
send multiple notifications using FCM, but within the same server side, saying that there is multiple notification types, probably with different data payloads and different notification titles, i want the the notification manager to be able to differentiate between each notification on the client side and send them to different Notification Channels?
EDIT
I tried setting notification_id and it does not work.
EDIT:
I can add a Data payload and restrict the notification in one of my classes and add a switch statement which will divide according to my Notification but i need to handle the data in the background and not foreground.
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
notificationSwitch(remoteMessage);
}
private void notificationSwitch(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
if (notification.getTitle().equals("TypeOne")){
TypeOneSendNotification(remoteMessage);
}
else{
TypeTwosendNotification(remoteMessage);
}
}
//Ofcourse we are able to switch based on the title, or different data payloads
EDIT:
I converted into a data payload and edited the notification title to be able to switch the above edit
Create the notification_key, which identifies the device group by mapping a particular group (typically a user) to all of the group's associated registration tokens. You can create notification keys on the app server or on Android client apps.
please check this link
https://firebase.google.com/docs/cloud-messaging/android/device-group
I got this working by implementing a switch statement on the title is merely a hack but it definetly works
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
notificationSwitch(remoteMessage);
}
and in my notification switch function as i already have remoteMessage as an argument i will just implement my senders.
private void notificationSwitch(RemoteMessage remoteMessage) {
RemoteMessage.Notification notification = remoteMessage.getNotification();
if(notification.getTitle() != null){
switch (notification.getTitle()) {
case "SenderOne":
SenderOneSendNotification(remoteMessage);
break;
case "SenderTwo":
SenderTwoSendNotification(remoteMessage);
break;
case "SenderThree":
SenderThreeSendNotification(remoteMessage);
break;
case "SenderFour":
SenderFourSendNotificaation(remoteMessage);
break;
default:
DefaultSender(remoteMessage);
break;
}
}
Answering the question in the title: FCM from multiple senders but within the same app
If sender == Firebase Project then I don't think it is possible.
If sender == a client inside the same Firebase Project (like an iOS app, multiple servers, another Android app), then you need to create those apps inside your Firebase Project (for iOS and Android apps) and use the server key (for the servers)
If sender == function (like "Update Entity A", "show notification for promotion", etc), then you can use the data field inside the notification. We usually add an action field inside the data field. Both the receiver (iOS or Android app) and the sender (usually a server, but can also be a mobile app) know the list of possible actions. Receivers know what to do when they receive them (if app receives "Update Entity A", then it starts the EntityAUpdateService).
I'm an Android app developer, and is developing an instant message app. The app has a notification problem on Android Oreo system, which shows persistent notification "App is running in the background" and cannot be cleared, and it's OK on system before Android Oreo.
Screenshot: The phone shows persistent notification App is running in the background
I find some discussion, such as Nexus Help Forum about this question, but it doesn't work in my phone's settings.
I want to know how to hide this notification programmatically and the app also can receive message instantly because it's an instant message app.
Any help is very appreciated.
The app has a notification problem on Android Oreo system, which shows persistent notification "App is running in the background" and cannot be cleared, and it's OK on system before Android Oreo.
You used startForeground() with a minimum-importance Notification.
I want to know how to hide this notification programmatically
Use startForeground() with a Notification that has higher than minimum importance. Or, do not use startForeground().
I find some install message apps such as WeChat, Facebook doesn't have this problem on Android Oreo
They are not using foreground services, presumably. For example, they might be using Firebase Cloud Messaging (FCM).
Before we talk about how to get rid of it, however, let’s talk about why it’s there in the first place.
Basically, in previous versions of Android, there was no real way of knowing if an app was running in the background doing a bunch of stuff it’s not supposed to be doing. In most scenarios, these misbehaving apps would wreak havoc on the battery by keeping the system awake—these are called “wakelocks.” In laymen’s terms, it was keeping the system from sleeping. That’s bad.
With Oreo, Google is calling out developers that let their apps do this sort of thing with the new notification. Essentially, if an app is running in the background and chewing up battery life, this new notification will tell you.
NOTE: There are a few legitimate scenarios where an app will continuously run in the background, like the VPN service running. Often, however, apps are running in the background unjustifiably.
It’s worth noting, though, removing the notification does not solve the issue. Period. There’s a reason this notification exists, and getting rid of it will do nothing to solve the underlying issue. You’ll either need to change a setting within the app or uninstall it altogether.
As long as you understand that and still want to remove it, let’s do this thing.
Because this is a relatively crucial system setting, there’s no way within Oreo itself to remove it. That makes sense.
But like with most things, the developer community has found a way to remove it, and developer iboalali released an app to do just that. It’s actually just called “Hide ‘running in the background’ Notification,” which is about as straightforward as an app name could ever be. Go ahead and give it an install.
Without root, there is no way to actually prevent Android System from displaying the persistent “app is running in the background” notification in Android 8.0 Oreo. Looking at the source code for the ForegroundServiceController, its implementation, and the ForegroundServiceDialog doesn’t really reveal anything we can take advantage of. Programatically nothing have been found so far.
Here's a Blog post that can help you
First, you must have the NotificationListenerService implementation. Second, in this service (after onListenerConnected callback), check the active ongoing notifications with packageName called 'android'. And check this notification's title is your app name or text value is 'App is running in the background' and snooze it.
public class NLService extends NotificationListenerService {
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {}
#Override
public void onListenerConnected() {
super.onListenerConnected();
checkOngoingNotification();
}
#Override
public void onNotificationPosted(StatusBarNotification sbn){
if(sbn.isOngoing()) {
checkOngoingNotification();
return;
}
}
private void checkOngoingNotification() {
StatusBarNotification[] activeNotifications = getActiveNotifications();
Log.i("NLService", "Active notifications size : " + activeNotifications.length);
for (StatusBarNotification statusBarNotification : activeNotifications) {
Log.i("NLService", "notification package : " + statusBarNotification.getPackageName());
Log.i("NLService", "notification id : " + statusBarNotification.getId());
Log.i("NLService", "notification key : " + statusBarNotification.getKey());
Log.i("NLService", "isOngoing : " + statusBarNotification.isOngoing());
Log.i("NLService", "isClearable : " + statusBarNotification.isClearable());
Log.i("NLService", "groupKey : " + statusBarNotification.getGroupKey());
Notification notification = statusBarNotification.getNotification();
CharSequence title = notification.extras.getCharSequence(Notification.EXTRA_TITLE);
CharSequence text = notification.extras.getCharSequence(Notification.EXTRA_TEXT);
if (title != null && text != null) {
Log.i("NLService", "title : " + title);
Log.i("NLService", "text : " + text);
if (statusBarNotification.getPackageName().equals("android") &&
(title.toString().contains("Your App Name") || text.toString().contains("App is running"))) {
long snoozLong = 60000L * 60L * 24L * 20L;
this.snoozeNotification(statusBarNotification.getKey(), snoozLong);
Log.i("NLService", "Snoozed notification : " + title);
}
}
}
}
It turns out startForeground() with channel's IMPORTANCE_MIN is not only one source of the notification. If you call startForeground() and give it notification without setSmallIcon() result will be the same.
https://android.googlesource.com/platform/frameworks/base/+/master/services/core/java/com/android/server/am/ServiceRecord.java#816
Also you could find in logs something like that:
ActivityManager: Attempted to start a foreground service (ComponentInfo{com.example.app/com.example.app.ExampleService}) with a broken notification (no icon: Notification(channel=channel_example pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE))
I'm building an application using Ionic Framework that implements a chat function similar to good-old facebook messenger, in that i want to notify users of a chat message, but if they view it elsewhere, i want to remove the notification from their home screen.
I'm using firebase as a back-end for push notifications (though that could be changed i suppose).
I know that you can't expire a remote notification, but i've been told you can expire + remove a local notification, so my question is - can i reliably receive a remote notification, create a local one, and display that, and then in response to a notification with a scope of 'expire' or 'remove', delete a local notification so that my users don't see a duplication of information?
Most plugins tend to detect the status of the app and add a remote notification to the homescreen with the info you've pushed by default, is there a way to avoid this?
Thanks guys.
EDIT:
- Local notifications: http://ionicframework.com/docs/native/local-notifications/
- Firebase cloud messaging: https://github.com/fechanique/cordova-plugin-fcm
As far as I can tell there're no plugins which accomplish all what you need. However..
can i reliably receive a remote notification, create a local one, and display that, and then in response to a notification with a scope of 'expire' or 'remove', delete a local notification so that my users don't see a duplication of information?
Most plugins tend to detect the status of the app and add a remote notification to the homescreen with the info you've pushed by default, is there a way to avoid this?
Yes, by using silent notifications and building the local notification by yourself.
For a project I'm working in, I modified the plugin cordova-plugin-fcm to add support for (local on demand) notifications dismiss/display, send multiple notifications to the cordova app, and some PRs that are not included yet. Also I build the notification by myself, to have full control of what is displayed. You can take a look at the code to get some ideas.
In brief it works like this:
Firstly, I send a "silent" push to the app, which is not displayed by Android:
{
"content_available": true, // IMPORTANT: For Apple -> content-available: 1, for firebase -> content_available: true
"priority": "high",
"to": "/topics/all", // or to a fcm token
"data"{
"title": "My title", // this implies that you display the notification by yourself
"body": "My body", // this implies that you display the notification by yourself
"type": "NEW_USER_MESSAGE", // only relevant to this project
"userId": "1", // only relevant to this project
"timestamp", "150000000"
}
}
Note: If the payload have the "notification": {} item, Android will display it on the system tray (if the app is in background).
https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
Secondly, when the push arrives to the app (in onMessageReceived()), I build the local notification, assigning it a TAG and an ID. This is the way you can use to dismiss it later.
For example, you could create a local notification with the TAG "NEW_USER_MESSAGE" and ID 1 (a constant indicating a state of the message, or the user ID for example). Also, Android will replace notifications with the same TAG and ID, so this is another way to automatically replace notifications (for example if you send a generic message, like "New update available").
public static String TYPE_NEW_USER_MESSAGE = "NEW_USER_MESSAGE";
public static String TYPE_USER_LEFT_ROOM = "USER_LEFT_ROOM";
NotificationManager notificationManager =
(NotificationManager) _ctx.getSystemService(Context.NOTIFICATION_SERVICE);
// based in the type of the message you've received, you can stylize the notification
if (type.equals( TYPE_USER_LEFT_ROOM )){
notificationBuilder.setColor(Color.RED);
notificationBuilder.setLights(Color.RED, 1000, 500);
}
else if (type.equals( TYPE_NEW_USER_MESSAGE )){
notificationBuilder.setColor(Color.BLUE);
notificationBuilder.setLights(Color.BLUE, 1000, 1000);
}
Notification n = notificationBuilder.build();
notificationManager.notify(type, userId, n);
One advantage of doing it in this way, is that you have full control of the notification to be displayed, so you can stylize it like you want.
If you want to discard expired messages, you can check out the elapsed time between the sent timestamp and the current timestamp:
java.util.Date now = new java.util.Date();
java.util.Date sent_timestamp = new java.util.Date( Long.valueOf(timestamp.toString()) );
final Long elapsed_time = ((now.getTime() - sent_timestamp.getTime()) / 1000);
Log.d(TAG, "New message. sent " + elapsed_time + "s ago");
Thirdly, when the user clicks on a notification Android will launch your app, and the plugin will send the payload of the push message to the cordova view (onNotificationReceived()).
Once your app is opened and you have received the push message, you can dismiss it adding a new action to the plugin:
onNotificationReceived(data){
if (data.wasTapped === true){
if (data.type === 'NEW_USER_MESSAGE'){
FCMPlugin.dismissNotification(NEW_USER_MESSAGE, 1);
}
}
}
The Android action:
else if (action.equals( ACTION_DISMISS_NOTIFICATION )) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
try{
Log.d(TAG, "FCMPlugin dismissNotificaton: " + args.getString(0)); //tag
NotificationManager nManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
nManager.cancel(args.getString(0)/*NEW_USER_MESSAGE*/, args.getInt(1) /*1*/);
Log.d(TAG, "FCMPlugin dismissNotificaton() to remove: " + id); //tag
callbackContext.success();
}catch(Exception e){
callbackContext.error(e.getMessage());
}
}
});
https://github.com/TrustedCircles/cordova-plugin-fcm/blob/master/src/android/FCMPlugin.java#L286
And the method exposed to the cordova app:
// dismisses a notification by tag+id
FCMPlugin.prototype.dismissNotification = function( tag, userId, success, error ){
exec(success, error, "FCMPlugin", 'dismissNotification', [tag, userId]);
}
https://github.com/TrustedCircles/cordova-plugin-fcm/blob/master/www/FCMPlugin.js#L65
The only tricky bit with notifications in cordova/ionic is the JS part receiving the notification and triggering the Android code.
I used https://github.com/phonegap/phonegap-plugin-push library and its pretty straight forward.
There is a callback when notifications are received in JS(Cordova/Ionic), use this to render you notifications locally in Android.
P.S: Basel's answer tells you how to clear your notifications, so I decided to leave that bit out.
I am using cordova plugin add phonegap-plugin-push plugin for push notification
In forground notification works fine.and i can handle event also.
When my app is in background then i got notification as well but on click of push notification my event is not fire.
I am using below code
$cordovaPushV5.initialize(options).then(function() {
// start listening for new notifications
$cordovaPushV5.onNotification();
// start listening for errors
$cordovaPushV5.onError();
// register to get registrationId
if (PNdeviceToken == null) //becuase registration will be done only the very first
{
$cordovaPushV5.register().then(function(registrationId) {
// save `registrationId` somewhere;
window.localStorage.setItem('PNdeviceToken', registrationId);
$rootScope.fcmToken = registrationId;
console.log(registrationId)
alert("first time registered id -- " + registrationId)
})
} else {
$rootScope.fcmToken = PNdeviceToken;
alert("already saved registered id -- " + $rootScope.fcmToken)
}
});
$rootScope.$on('$cordovaPushV5:notificationReceived', function(event, data) {
console.log(event)
console.log(data)
})
When i tap on background push notiction then $cordovaPushV5:notificationReceived event not fire, How can I solve this problem?
How can i handle background push notification event?
I had the Same issue and got it resolved it after 2 days of research.
Handling the notification events is same whether the app is in foreground or background.
We have to set "content-available" : "1" in the data field while pushing notifications. Else it wont call notificationReceived event if app is in background.
Also note this is not possible as of now through Google Firebase Console.
We have to send our custom payload messages (data or notification or both) seperately using any one of the firebase servers.
Detailed info can be found on the plugin's GitHub Docs Page on background notifications.
Quoting from there -
On Android if you want your on('notification') event handler* to be called when your app is in the background it is relatively simple.
First the JSON you send from GCM will need to include "content-available": "1". This will tell the push plugin to call your on('notification') event handler* no matter what other data is in the push notification.
*on('notification') event handler = $cordovaPushV5:notificationReceived event in your case.
See this answer for sending custom payload messages using PHP and NodeJS
In My android app I want to show history of notification, so I want to capture all notifications title and message.
I am using accessibilityservice and able to fetch title using parcelable data but not get original message text.
If you have any ideal please help me.
I found similar answer here
another way is -
Parcelable data = event.getParcelableData();
if (data instanceof Notification) {
Log.d("Tortuga","Recieved notification");
Notification notification = (Notification) data;
Log.d("Tortuga","ticker: " + notification.tickerText);
Log.d("Tortuga","icon: " + notification.icon);
Log.d("Tortuga", "notification: "+event.getText());
hope it will give help in finding your solution.