Firebase cloud messaging stopped with upgrade to Oreo - android

I was just getting started with Android development and managed to get Firebase messaging working after running the wizard. I was able to receive background notifications on my Nexus 5X running Nougat. But then my 5X upgraded to Oreo and Firebase notifications haven't worked since. I've heard about background execution limitations, but since I'm just getting started I don't know what I actually have to do to get it working again. Are there any write ups on this? I tried a new project from scratch hoping the wizard had been updated, but no change. I was using application broadcast messages and topic subscription messages, no device token used.

When you target Android 8.0 (API level 26), you must implement one or more notification channels to display notifications to your users. If you don't target Android 8.0 (API level 26) but your app is used on devices running Android 8.0 (API level 26), your app behaves the same as it would on devices running Android 7.1 (API level 25) or lower.
If you change your target version into Android 8.0 (API level 26), you have to do the following change
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String id = "id_product";//This will be enter when creating Builder
// The user-visible name of the channel.
CharSequence name = "Product";
// The user-visible description of the channel.
String description = "Notifications regarding our products";
int importance = NotificationManager.IMPORTANCE_MAX;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
// Configure the notification channel.
mChannel.setDescription(description);
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);
notificationManager.createNotificationChannel(mChannel);
}
Change your Builder constructor
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(applicationContext, **ID you have put in the Notification Channel**)
Finally you can create the notification from the Builder
notificationManager.notify("0", notificationBuilder.build())
Ref: https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels

Please use a recent version of the FCM sdk.
FCM sdk introduced support for Android O in April, if you are using older version your app would not receive messages in Android O and could potentially crash.
See the latest release here: https://firebase.google.com/support/release-notes/android
PS: firebase SDK has been moved to the Google Maven repository.
Be sure to check out the latest instructions here:
https://firebase.google.com/docs/android/setup#manually_add_firebase

Related

startForegroundService - Specify a custom 'background app running..' notification?

I have a foreground service that I launch via startForegroundService.
All works great.
The only thing I am unable to figure out is how to / if its possible to customize the "...is running in the background' notification.
The notification I am sending over to startForeground looks like this:
Notification notification = notificationBuilder.setOngoing(true)
.setCategory(NotificationCompat.CATEGORY_SERVICE)
.setSmallIcon(R.drawable.ic_notif_icon)
.setContentTitle("My title")
.setContentText("My content")
.build();
startForeground(1, notification);
I am then presented by android with the standard "...running in background" notification, with my custom one no where to be seen.
Am I doing something wrong?
Not looking to hide it or anything nefarious, but would like to use it more as a status of what the background service is actually doing... Which sounds like an ideal use case for this sort of thing.
For what its worth, I am running Android 8 and targeting SDK 26 as that is the latest available for my handset.
Thanks!
As per the Create and Manage Notification Channels guide:
Starting in Android 8.0 (API level 26), all notifications must be assigned to a channel.
Your notification is not appearing because you do not set a notification channel as per the note on that same page:
Caution: If you target Android 8.0 (API level 26) and post a notification without specifying a notification channel, the notification does not appear and the system logs an error.
Note: You can turn on a new setting in Android 8.0 (API level 26) to display an on-screen warning that appears as a toast when an app targeting Android 8.0 (API level 26) attempts to post without a notification channel. To turn on the setting for a development device running Android 8.0 (API level 26), navigate to Settings > Developer options and enable Show notification channel warnings.

Enable all notification settings by default in my android app

Is there any way I can enable all notification settings by default when my app gets installed ?
Users are receiving notifications but sound is disabled by default and we need to manually enable it on the device. Not all users can do this manually. It would be great to know if there is any way we can check all these things when our app gets installed like WhatsApp or Telegram (they have everything checked by default)
Try using with this below permission in AndroidManifest file
<uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY"/>
and set notification priority for both below and above Oreo versions IMPORTANCE_HIGH for Oreo and above, and PRIORITY_HIGH or PRIORITY_MAX for below Oreo versions
Reference link for priority note
Priority for version below Oreo
mBuilder.setSmallIcon(R.drawable.app_logo)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.setContentTitle(title)
.setStyle(bigPictureStyle)
.setSound(soundUri)
.setPriority(NotificationCompat.PRIORITY_HIGH) // prirority here for version below Oreo
.setWhen(System.currentTimeMillis())
.setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.app_logo))
.setContentText(message)
.build();
Priority for Oreo and above
Refer this link
WhatsApp, Telegram, Snapchat, and so on. These are all white-listed apps which means the package names are hardcoded at OS level to allow some of the permissions enabled by default. Our apps are not so. Hence, we need the user to enable those permissions manually.
You can check this by yourself. Create a new android application give the package name of the Telegram application(org.telegram.messenger) and just run it. Don't do any code at all, and no need to open the app too. Simply go to the notification settings of the newly created application, where you find all the permissions enabled by default.
Hope you got the answer.
Android 8 or higher you need to use NotificationChannel to enable sound, vibration, sound etc.
Uri notification_sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
notificationChannel.setSound(notification_sound, attributes);//for enable sound
notificationChannel.enableLights(true);
notificationManager.createNotificationChannel(notificationChannel);
}
but in Redmi note 5 pro (MIUI 10.2.1.0) still notification sound is disabled. I think there is a bug in MIUI. Run this same code in mi A1(Android one mobile) everything fine. It works.
refer this link to know more about Notification Channel

Delphi - New apps on Google Play must target Android 8 (API level 26) - PUSH notification in background

From August 2018, all new apps on Google Play must target Android 8 (API level 26) or higher, and from November 2018, all app updates on Google Play must of the same apps on Google Play.
Right now the only way you have to upload a new App that target Android 8 is to edit the file AndroidManifest.template.xml and replace
targetSdkVersion = "% targetSdkVersion%"
by:
Android: targetSdkVersion = "26"
The problem is that from that moment the app has the restrictions introduced by Android O. The permissions considered as dangerous (camera, location, SMS, ...) will not be granted to the app by the mere fact of including them in the AndroidManifest file. Goodbye to the camera, to the GPS, ...
In this web, you can following a few simple steps to start requesting permissions from the user:
http://delphiworlds.com/2018/05/targeting-android-8-and-higher/
HOWEVER, target Android 8 has many more implications. My application, for the mere fact of changing the targetSDKVersion from 25 to 26 DOES NOT RECEIVE PUSH NOTIFICATIONS when the application is not running (or in background).
My test is simple: I change the targetSDK and it does not work anymore. I rewind and it works again, both with the app running and with the app in background or closed.
The key is the change of TARGETSDKVERSION because I have always tried selecting the SDK 24.3.3 in the SDK Manager.
I think one of the main reasons is the disappearance of the Background Services in Android O, as they explain in https://blog.klinkerapps.com/android-o-background-services/ But I’m not sure.
MY BIG PROBLEM.
I just uploaded an Android 7 (Level 25) app to Google Play. The problem is that as of November 2018 I will NOT be able to upload updates if I do not change TARGETSDKVERSION to Level 26. But if I do ... I will stop receiving PUSH notifications, and without PUSH notifications, my App DOES NOT WORK FOR ANYTHING.
I confess that I'm a little scared with this
I'm sorry for my English.
Thank you VERY much.
You will have to make sure that your notification is a high priority, FCM will post it immediately
FCM attempts to deliver high priority messages immediately, allowing the FCM service to wake a sleeping device when necessary and to run some limited processing (including very limited network access). High priority messages generally should result in user interaction with your app. If FCM detects a pattern in which they don't, your messages may be de-prioritized
If your users interact with the notifcaiton FCM will not delay it. Background services may not be allowed in some cases in Android O but it doesn't mean you cannot send notifications
Also your notification will not be displayed if your not using notification channels, You can use this code to create notification channels
public void initChannels(Context context) {
if (Build.VERSION.SDK_INT < 26) {
return;
}
NotificationManager notificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("default",
"Channel name",
NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription("Channel description");
notificationManager.createNotificationChannel(channel);
}

FCM topic notifications being received on emulator but no other device

I'm having an issue where Firebase Cloud Messaging will send notifications to the emulator (Android 7.0, with play services installed) but not to any device (Google Pixel 2, Samsung s9+ , Huawei MATE 10) running Android 8+. I initially thought it might have been a power saving issue closing off the listener but even when I remove optimisation from the app, it still doesn't come through.
I know the downstream request is being sent successfully and I'm receiving status code 200 from the response (and of course because the notification is being received on the emulator). When debugging on the devices however, the onMessageReceived in my FCMService class is not being called at all, whether in the foreground or background.
It's definitely registered in the manifest and each device is using the same version of the application:
<service android:name=".services.FCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
If there is any more information required, let me know. Thanks in advance.
Why was this happening?
I realised when I entered the version numbers into the question that it would have to do with Android Oreo. I was searching for issues with FCM and not recent changes the how notifications work. In Android 26+, notification channels have been introduced and are required if your app is targeting 26 and above (which I was). Because I updated the target SDK without thinking, I of course got hit with no notifications.
How to fix it
The easiest way is to just target API 25 but that's not ideal.
What you can do is register notification channels. I've done this where I handle my notifications (so onMessageReceived) as it doesn't register the same channel more than once.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Set the default notification channel settings to be that of alert
int importance = NotificationManager.IMPORTANCE_HIGH;
String channel_id = ALERT_CHANNEL_ID;
CharSequence channel_name = ALERT_CHANNEL_NAME;
String channel_desc = ALERT_CHANNEL_DESC;
// Logic to change the channel id, name and desc if required
// Build and modify channel settings
NotificationChannel channel = new NotificationChannel(channel_id, channel_name, importance);
channel.setDescription(channel_desc);
channel.enableLights(true);
channel.enableVibration(true);
// Create the notification channel
notificationManager.createNotificationChannel(channel);
}
Then you want to change the notification builder:
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channel_id)

How to Disable my App from Appearing on Android Wear Devices?

How to Disable my App from Appearing on Android Wear Devices?
It seems that my app integrates automatically with Android Wear (notifications mirroring) but I want to disable it...
To keep your app's [phone] notification from being mirrored to Android Wear, call setLocalOnly(true) on the Notification.Builder (or NotificationCompat.Builder, if you're targeting SDK < 20) that you use to create the notification.
For example:
NotificationCompat.Builder bob = new NotificationCompat.Builder(context)
.setContentTitle(title)
.setContentText(content)
.setLocalOnly(true);
(the last line is the important one)
Documented here: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setLocalOnly(boolean)

Categories

Resources