Samsung S8/S5/J2/Note3
are getting Firebase Push Notification
successfully either app is killed, in background or foreground,
but Infinix Note 5 (Android One- Oreo)
and Oppo F9(Oreo)
are not getting push notification if app is killed, they work fine if app is in background or foreground.
I went through a lot of articles, this and this, mentions that Chinese phones haves this problem, and there are some work around from user side to make these notification work on their phones.
but i want to know if anything is possible from development side to make notification work on each and every android device.
I am targeting API 27, and this is my code
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String from = remoteMessage.getFrom();
Map data = remoteMessage.getData();
JSONObject jsonObject = new JSONObject();
Set<String> keys = remoteMessage.getData().keySet();
for (String key : keys) {
try {
jsonObject.put(key, remoteMessage.getData().get(key));
} catch (JSONException e) {
e.printStackTrace();
}
}
message= jsonObject.getString("message");
sendNotification(message, urlToOpen);
private void sendNotification(final String msg, final String urlToOpen) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.notification_channel_general);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("App Name")
.setContentText(msg)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setPriority(Notification.PRIORITY_MAX)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"App Channel",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
}
notificationManager.notify(0, notificationBuilder.build());
Gradle
compileSdkVersion 27
defaultConfig {
applicationId "com.myapp.app"
minSdkVersion 19
targetSdkVersion 27
versionCode 2
versionName "1"
multiDexEnabled true
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
// Disable fabric build ID generation for debug builds
ext.enableCrashlytics = false
}
}
implementation 'com.google.firebase:firebase-messaging:15.0.0'
implementation 'com.google.android.gms:play-services-location:15.0.0'
implementation 'com.google.android.gms:play-services-auth:15.0.0'
implementation 'com.google.android.gms:play-services-basement:16.0.1'
implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0'
implementation 'com.google.android.gms:play-services-stats:16.0.1'
implementation 'com.google.android.gms:play-services-tasks:16.0.1'
implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0'
implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0'
implementation 'com.google.android.gms:play-services-maps:16.0.0'
Source : Enable background services and Jobs (or FCM) in Chinese ROMs
Infinix Note 5 (Android One- Oreo) and Oppo F9(Oreo) are not getting push notification if app is killed, they work fine if app is in background or foreground.
Chinese ROMs using (Oxygen OS, MIUI etc) when the apps are swiped from the recent app tray your app gets terminated ( similar to Force Stop). And due to this every task running in the background like Services, Jobs gets killed with the app. Even High priority FCM doesn’t see the daylight in Chinese ROMs
Real time problems :
1) You can’t fetch location of the user. So, no app would work properly if it depends on real-time location
2) You can’t recevie the FCM high priority notifications
3) No time specific task/Job would execute if app is not in the tray and many more ….
Solution
user needs to enable auto start in settings for app to keep background service/Job running.By default it is off.To do this in some of the devices we can use,
Sample code
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent();
String manufacturer = android.os.Build.MANUFACTURER;
switch (manufacturer) {
case "xiaomi":
intent.setComponent(new ComponentName("com.miui.securitycenter",
"com.miui.permcenter.autostart.AutoStartManagementActivity"));
break;
case "oppo":
intent.setComponent(new ComponentName("com.coloros.safecenter",
"com.coloros.safecenter.permission.startup.StartupAppListActivity"));
break;
case "vivo":
intent.setComponent(new ComponentName("com.vivo.permissionmanager",
"com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
break;
}
List<ResolveInfo> arrayList = getPackageManager().queryIntentActivities(intent,
PackageManager.MATCH_DEFAULT_ONLY);
if (arrayList.size() > 0) {
startActivity(intent);
}
}
}