Android able to receive notification when app is in foreground and background but when I kill it causing unable to receive notification.
I'm not sure what part was wrong.
code in AndroidManifest.xml
<service
android:name=".MyFirebaseMessagingService"
android:exported="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
MyFirebaseMessagingService.java
I don't create MyFirebaseIdService.java for refresh token. I use onNewToken(String mToken) in this class instead.
public class MyFirebaseMessagingService extends FirebaseMessagingService {
#Override
public void onNewToken(String mToken) {
super.onNewToken(mToken);
Log.e("NEW_TOKEN",mToken);
}
Here is my onMessageReceived code.
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Log.d(TAG, "From: " + remoteMessage.getFrom());
RemoteMessage.Notification notification = remoteMessage.getNotification();
Map<String, String> data = remoteMessage.getData();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
sendNotification(notification, data);
}
}
sendNotification code, I attempt make it to receive both data payload and notification payload but it still not work.
#RequiresApi(api = Build.VERSION_CODES.O)
private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
String title;
String body;
if (data != null) {
title = data.get("title");
body = data.get("body");
} else {
title = notification.getTitle();
body = notification.getBody();
}
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
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);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "channel_id")
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setContentIntent(pendingIntent)
.setLargeIcon(icon)
.setColor(Color.BLUE)
.setSmallIcon(R.mipmap.ic_launcher);
try {
String picture_url = data.get("picture_url");
if (picture_url != null && !"".equals(picture_url)) {
URL url = new URL(picture_url);
Bitmap bigPicture = BitmapFactory.decodeStream(url.openConnection().getInputStream());
notificationBuilder.setStyle(
new NotificationCompat.BigPictureStyle().bigPicture(bigPicture).setSummaryText(notification.getBody())
);
}
} catch (IOException e) {
e.printStackTrace();
}
notificationBuilder.setDefaults(Notification.DEFAULT_VIBRATE);
notificationBuilder.setLights(Color.RED, 1000, 300);
NotificationManagerCompat managerCompat = NotificationManagerCompat.from(this);
create channel code
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(
"channel_id", "channel_name", NotificationManager.IMPORTANCE_DEFAULT
);
channel.setDescription("channel description");
channel.setShowBadge(true);
channel.canShowBadge();
channel.enableLights(true);
channel.setLightColor(Color.RED);
channel.enableVibration(true);
channel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
channel.setVibrationPattern(new long[]{100, 200, 300, 400, 500});
managerCompat.createNotificationChannel(channel);
}
managerCompat.notify(101,notificationBuilder.build());
}
}
You cannot receive notification when killed app for one cause:
Some manufacturers, like HUAWEI, OPPO ecc... make unavailable to push notifications while app is not running, this in order to save your battery life. Of course, important app (WhatsApp, Instagram ecc...) make exception by default. In other hand, there's a way to fix it, every app can get this permission manually via settings->battery (it changes phone by phone).
Now you have to find this permission which affords app to push notifications without running. A tip: when I've tick this one, it doesn't work; I had to unistall app via settings->app, then re-install it again
Related
I added the following pice in the manifest to call the onMessageReceived function when a firebase message comes in:
<service android:name=".Notifications"
android:exported="true">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
Here is my onMessageReceive function:
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("NotificationService", "Notification received!");
Map<String, String> data = remoteMessage.getData();
String deep_link = data.get("deep_link");
String title = data.get("title");
String body = data.get("body");
Log.d("NotificationService", "Notification received!");
NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.context.getApplicationContext(), "1");
builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
Intent intent = new Intent(new MainActivity(), Uri.parse(deep_link));
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
builder.setContentIntent(pendingIntent);
builder.setLargeIcon(BitmapFactory.decodeResource(MainActivity.context.getResources(), R.mipmap.ic_launcher));
builder.setContentTitle(title);
builder.setContentText(body);
NotificationManager notificationManager = (NotificationManager) MainActivity.context.getSystemService(MainActivity.context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
String channelId = "1";
NotificationChannel channel = new NotificationChannel(
channelId,
"1",
NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(channel);
builder.setChannelId(channelId);
}
// Will display the notification in the notification bar
notificationManager.notify(1, builder.build());
}
But the function is never called does someone know where the problem lays?
I tried to send the notification with and without notification payload, tried the app in background state and foreground state, tried killing the app and then sending the notification
When clicking the notification it doesnt open the app or anything
Hi guys as the title suggests I have a problem regarding Firebase Cloud Messaging, everything works perfectly even when the device does not open the app for a long time, receives notifications perfectly, but the problem occurs when I schedule the daily notification via the console firebase li the first time of sending works and sends the notification to all devices, while the next day at the scheduled time the notification is no longer received and this problem I do not know how to solve it, I have tried everything without any solution, the daily notifications do not are received, can you help me thanks.
#SuppressLint("MissingFirebaseInstanceTokenRefresh")
public class MyFirebaseMessagingService extends FirebaseMessagingService {
/*public static int NOTIFICATION_ID = 1;*/
public static String NOTIFICATION = "notification";
public static String NOTIFICATION_CHANNEL_ID = "com.fourapper.forpaper.channel";
#Override
public void onMessageReceived(#NonNull RemoteMessage remoteMessage) {
generateNotification(remoteMessage.getNotification().getBody(), remoteMessage.getNotification().getTitle());
}
private void generateNotification(String body, String title) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
/*PendingIntent pendingIntent = PendingIntent.getActivities(this, 0 , new Intent[]{intent}, PendingIntent.FLAG_ONE_SHOT);*/
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
/*NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this);*/
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setSmallIcon(R.drawable.ic_logo_forpapaer)
.setVibrate(new long[]{100, 500})
.setSound(soundUri)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("info")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = null;
if (intent.hasExtra(NOTIFICATION)) {
notification = intent.getParcelableExtra(NOTIFICATION);
}
int notificationId = (int) System.currentTimeMillis();
notificationManager.notify(notificationId, notification);
/*if(NOTIFICATION_ID > 2147483646){
NOTIFICATION_ID = 0;
}*/
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Notification", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription("Channel");
notificationChannel.setBypassDnd(true);
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[]{ 0, 500});
notificationChannel.enableVibration(true);
notificationChannel.setImportance(NotificationManager.IMPORTANCE_HIGH);
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(notificationId, builder.build());
}
}
below the token
public class GettingDeviceTokenService extends FirebaseInstanceIdService {
#Override
public void onTokenRefresh() {
String deviceToken = FirebaseInstanceId.getInstance().getToken();
assert deviceToken != null;
Log.d("Device Token", deviceToken);
}
}
Below are the photos of the firebase cloud messaging console for scheduled daily sending at 12:00 AM Recipient Time Zone
sending setting change screen
My app's main functionality is push-notification messages sent from remote server. I am using FCM as a message delivery service. My problem is that notifications come without any sound on Xiaomi Mi 9 Lite (Android 9/MIUI 11). However, on Xiaomi Redmi Note 5 (Android 9/MIUI 10) sound works fine and on Samsung Galaxy S7 Edge (Android 8) as well. I created MessagingService which extends FirebaseMessagingService and notification channel as written in documentation.
Here is my code:
public class MessagingService extends FirebaseMessagingService {
private static String channelId;
private NotificationManager notificationManager;
private NotificationChannel notificationChannel;
private NotificationCompat.Builder notificationBuilder;
private MessagesViewModel viewModel;
public MessagingService() { }
#Override
public void onCreate() {
super.onCreate();
channelId = getResources().getString(R.string.default_notification_channel_id);
notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
final Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
notificationBuilder = new NotificationCompat.Builder(this, channelId);
notificationBuilder.setSmallIcon(R.raw.metrial_message_icon);
notificationBuilder.setAutoCancel(false);
notificationBuilder.setSound(soundUri);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
final AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
String name = getString(R.string.channel_name);
String description = getString(R.string.channel_description);
int importance = NotificationManager.IMPORTANCE_HIGH;
notificationChannel = new NotificationChannel(channelId, name, importance);
notificationChannel.setDescription(description);
notificationChannel.enableLights(true);
notificationChannel.setShowBadge(true);
notificationChannel.setSound(soundUri, audioAttributes);
notificationManager.createNotificationChannel(notificationChannel);
notificationBuilder.setChannelId(channelId);
}
else {
notificationBuilder.setPriority(NotificationCompat.PRIORITY_HIGH);
notificationBuilder.setBadgeIconType(NotificationCompat.BADGE_ICON_SMALL);
notificationBuilder.setLights(Color.WHITE, 500, 5000);
}
viewModel = new MessagesViewModel(getApplication());
}
#Override
public void onDestroy() {
super.onDestroy();
}
#Override
public void onNewToken(#NonNull String s) {
super.onNewToken(s);
logger.info("onNewToken()");
ConnectionParameters.getInstance().setToken(s);
MyPrefs.getInstance(getApplicationContext()).putString(Constants.TOKEN, s);
}
#Override
public void onMessageReceived(#NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
final String messageId = remoteMessage.getData().get("message_id");
final String title = remoteMessage.getData().get("title");
final String body = remoteMessage.getData().get("body");
if (messageId != null && title != null && body != null) {
final Message message = new Message();
message.setMessageId(messageId);
message.setTitle(title);
message.setContent(body);
message.setTimestamp(new Date());
try {
message.setNotificationId((int)viewModel.insert(message));
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
}
logger.info("onMessageReceived(): notificationId=" + message);
if (MyPrefs.getInstance(getApplicationContext()).getBoolean(Constants.ENABLE_PUSH)) {
notificationBuilder.setContentTitle(title);
notificationBuilder.setContentText(body);
final Intent notifyIntent = new Intent(this, MessageInfoActivity.class);
notifyIntent.putExtra(Constants.ARG_MESSAGE_OBJECT, message);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(notifyIntent);
PendingIntent pendingActivityIntent =
stackBuilder.getPendingIntent(message.getNotificationId(), PendingIntent.FLAG_UPDATE_CURRENT);
notificationBuilder.setContentIntent(pendingActivityIntent);
final Notification notification = notificationBuilder.build();
notification.defaults = Notification.DEFAULT_SOUND|Notification.DEFAULT_LIGHTS;
notificationManager.notify(message.getNotificationId(), notification);
}
}
}
private final Logger logger = LoggerFactory.getLogger(getClass());
}
And in Settings->Notifications I got the following parameters:
And inside my push-notifications-channel sound is enabled but whenever a message comes, it seems like app notification settings override parameters in notification channel.
There should be some solution because in popular apps such WhatsApp, Telegram, etc., these switches are enabled after installation (by default). Hope, someone helps!
As nobody provided better solution, I guess there is no way to allow sound/badge counter/floating notifications programmatically on MIUI (and mostly on other Chinese OEMs). It is a user's privilege to turn these settings on manually. Therefore, to enhance UX, it is important to decrease the quantity of "clicks" as much as possible. So, we could provide a dialog describing how to enable the features above with button leading to App's settings.
Namely, to open a notification settings page through Intent, do the following:
final Intent notificationSettingsIntent = new Intent();
notificationSettingsIntent
.setAction("android.settings.APP_NOTIFICATION_SETTINGS");
notificationSettingsIntent
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationSettingsIntent.putExtra(
"android.provider.extra.APP_PACKAGE",
activity.getPackageName());
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
notificationSettingsIntent.putExtra(
"app_package",
activity.getPackageName());
notificationSettingsIntent.putExtra(
"app_uid",
activity.getApplicationInfo().uid);
}
activity.startActivityForResult(
notificationSettingsIntent,
NOTIFICATIONS_SETTINGS_REQUEST_CODE);
and you could open a dialog with button "Open notification settings" clicking on which triggers the code snippet above.
I'm trying to use FCM and the notifications only works when I'm not using the application .
when I send notification from device A to device B , then device B receive the message and "show the notification pop with the default sound" and every thing is good... (this happens when device B is not using the application).
when I send notification from device A to device B , then device B receive the message in the onMessageReceived() method but "does not show the notification pop with the default sound".. (this happens when device B is using the application, I mean when the application is open and is being used).
this is my code
FireIDService.java
public class FireIDService extends FirebaseInstanceIdService {
private final String TAG = "FireIDService";
#Override
public void onTokenRefresh() {
String tkn = FirebaseInstanceId.getInstance().getToken();
Log.d("Not","Token ["+tkn+"]");
sendRegistrationToServer(tkn);
}
private void sendRegistrationToServer(String token) {
saveDeviceToken(token);
}
private void saveDeviceToken(String deviceToken) {
//some code..
if(response.body().getStatus() == 1){
doStuff();
}
//some code...
}
#Override
public void onFailure(Call<SaveDeviceTokenResponse> call, Throwable t) {
//code...
}
});
}
private void doStuff(){
Intent intent = new Intent(this, SplashActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("FCM Message")
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1410 /* ID of notification */, notificationBuilder.build());
}
}
FireBaseMsgService.java
public class FireBaseMsgService extends FirebaseMessagingService{
private final String TAG = "FireBaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "test")
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_background))
.setContentText(remoteMessage.getNotification().getBody())
.setAutoCancel(true)
.setColor(0xffff7700)
.setVibrate(new long[]{100, 100, 100, 100})
.setPriority(Notification.PRIORITY_MAX)
.setSound(defaultSoundUri);
Intent resultIntent = new Intent(this, SplashActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(SplashActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notificationBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notificationBuilder.build());
}
}
this is what added to the AndroidManifest.xml file
<service android:name=".FireIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service android:name=".FireBaseMsgService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
and this is the Notify class to be execute
public class Notify extends AsyncTask<Void,Void,Void>{
private String tkn;
private String title;
private String body;
public Notify(String tkn, String title, String body){
this.tkn = tkn;
this.title = title;
this.body = body;
}
#Override
protected Void doInBackground(Void... voids) {
Log.e("Token: ", tkn);
Log.e("Title: ", title);
Log.e("Body: ", body);
try {
URL url = new URL("https://fcm.googleapis.com/fcm/send");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization","key=KEY_HERE");
conn.setRequestProperty("Content-Type", "application/json");
JSONObject json = new JSONObject();
json.put("to", tkn);
JSONObject info = new JSONObject();
info.put("title", title); // Notification title
info.put("body", body); // Notification body
info.put("priority", "high");
info.put("show_in_foreground", "true");
json.put("notification", info);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}
catch (Exception e)
{
Log.d("Error",""+e);
}
return null;
}
}
If you are using android 8.0+. You need to specify channelId for notification.
When your app is in background (as mention in your first case), the push notification is recieved in system notification tray, rather than your FireBaseMsgService, and it is handled automatically by the system by channelId is genreated by system itself.
When your app is in foreground (second case) your FireBaseMsgService is executed and have to create notification channelId
I have implemented FCM Push notifications in my Android App.
I get the all the notification JSON in data payload. And I have 'not added' 'Notification' tag on api. So in all the states (Foreground/Background/Killed) I got the notification in data payload only.
It is Working fine on Non-Customised OS phones like Moto, Google etc, In All States when an app is Foreground/Background/Killed. But the Problem is when I testing on the customised OS phones like Oppo, Vivo or MIUI the notification is arrived only when the app is in Foreground or Background (App is in Memory), Not arrived/appearing when App is "killed" (Not in Memory).
What should I do?
In any case, thank you for your time.
public class MyFirebaseMessagingService extends FirebaseMessagingService{
private static final String TAG = "MyFirebaseMsgService";
/**
* Called when message is received.
*
* #param remoteMessage Object representing the message received from Firebase Cloud Messaging.
*/
// [START receive_message]
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
// [START_EXCLUDE]
// There are two types of messages data messages and notification messages. Data messages are handled
// here in onMessageReceived whether the app is in the foreground or background. Data messages are the type
// traditionally used with GCM. Notification messages are only received here in onMessageReceived when the app
// is in the foreground. When the app is in the background an automatically generated notification is displayed.
// When the user taps on the notification they are returned to the app. Messages containing both notification
// and data payloads are treated as notification messages. The Firebase console always sends notification
// [END_EXCLUDE]
// TODO(developer): Handle FCM messages here.
Log.e(TAG, "From: " + remoteMessage.getFrom());
// Check if message contains a data payload.
if (remoteMessage.getData().size() > 0)
{
Log.d(TAG, "Message data payload: " + remoteMessage.getData());
if (/* Check if data needs to be processed by long running job */ true) {
// For long-running tasks (10 seconds or more) use Firebase Job Dispatcher.
scheduleJob();
} else {
// Handle message within 10 seconds
handleNow();
}
if (remoteMessage.getNotification()!=null)
sendNotification(remoteMessage.getNotification().getBody());
else
sendNotification("Body");
}
// Check if message contains a notification payload.
if (remoteMessage.getNotification() != null)
{
Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());
if (remoteMessage.getNotification()!=null)
sendNotification(remoteMessage.getNotification().getBody());
else
sendNotification("Body");
}
// Also if you intend on generating your own notifications as a result of a received FCM
// message, here is where that should be initiated. See sendNotification method below.
}
// [END receive_message]
/**
* Schedule a job using FirebaseJobDispatcher.
*/
private void scheduleJob() {
// [START dispatch_job]
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job myJob = dispatcher.newJobBuilder()
.setService(MyJobService.class)
.setTag("my-job-tag")
.build();
dispatcher.schedule(myJob);
// [END dispatch_job]
}
/**
* Handle time allotted to BroadcastReceivers.
*/
private void handleNow() {
Log.d(TAG, "Short lived task is done.");
}
/**
* Create and show a simple notification containing the received FCM message.
*
* #param messageBody FCM message body received.
*/
private void sendNotification(String messageBody)
{
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
PendingIntent.FLAG_ONE_SHOT);
String channelId = getString(R.string.default_notification_channel_id);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("FCM Message")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId,
"Channel human readable title",
NotificationManager.IMPORTANCE_DEFAULT);
if (notificationManager != null) {
notificationManager.createNotificationChannel(channel);
}
}
if (notificationManager != null) {
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}
}
}
My AndroidManifest.xml file as below:
<!-- [START firebase_iid_service] -->
<service
android:name=".Firebase.FirebaseId">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
</intent-filter>
</service>
<service
android:name="Firebase.MyFirebaseMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<!-- [END firebase_iid_service] -->
<!--
Set custom default icon. This is used when no icon is set for incoming notification messages.
-->
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="#drawable/ic_launcher_background" />
<!--
Set color used with incoming notification messages. This is used when no color is set for the incoming
notification message.
-->
<meta-data
android:name="com.google.firebase.messaging.default_notification_color"
android:resource="#color/colorAccent" />
<!-- [START fcm_default_channel] -->
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel_id"
android:value="#string/default_notification_channel_id" />
<!-- [END fcm_default_channel] -->
Well i have found a solution to this problem. Write a custom service for your application which continuously running in background and write one Broadcast Receiver to restart the service once it is killed. This works fine for me. I have tested this on Vivo, Oppo, Redmi phones. It is working!
My Service Code is as below--
public class MyService extends Service
{
private static final String TAG = "MyService";
#Override
public void onStart(Intent intent, int startId)
{
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
#Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
#Override
public void onCreate()
{
super.onCreate();
}
#Override
public int onStartCommand(Intent intent, int flags, int startId)
{
//call to onTaskRemoved
onTaskRemoved(intent);
//return super.onStartCommand(intent, flags, startId);
Toast.makeText(this, "Service Started!", Toast.LENGTH_SHORT).show();
return START_NOT_STICKY;
}
#Nullable
#Override
public IBinder onBind(Intent intent)
{
return null;
}
#Override
public void onDestroy()
{
Toast.makeText(this, "Service Destroyed!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("com.myapp.startservice");
//Intent intent = new Intent("android.intent.action.BOOT_COMPLETED");
intent.putExtra("yourvalue", "torestore");
sendBroadcast(intent);
super.onDestroy();
}
#Override public void onTaskRemoved(Intent rootIntent)
{
Log.e("onTaskRemoved", "Called!");
//thread = new Thread(this);
//startThread();
/*Intent alarm = new Intent(this.getApplicationContext(), MyBroadCastReceiver.class);
boolean alarmRunning = (PendingIntent.getBroadcast(this.getApplicationContext(), 0, alarm, PendingIntent.FLAG_NO_CREATE) != null);
//if(!alarmRunning)
{
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), 0, alarm, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), 10000, pendingIntent);
}
}*/
//send broadcast to your BroadcastReciever
Intent intent = new Intent("com.myapp.startservice"); //unique String to uniquely identify your broadcastreceiver
//Intent intent = new Intent("android.intent.action.BOOT_COMPLETED");
intent.putExtra("yourvalue", "torestore");
sendBroadcast(intent);
//intent to restart your service.
Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
restartServiceIntent.setPackage(getPackageName());
PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
if (alarmService != null) {
alarmService.set(
AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + 1000,
restartServicePendingIntent);
}
super.onTaskRemoved(rootIntent);
}}
My BroadcastReceiver is as Below --
public class MyBroadCastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Log.e("MyBroadCastReceiver", "onReceive");
//if ("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
{
Intent service = new Intent(context, MyService.class);
context.startService(service);
Log.e("BootCompleteReceiver", " __________BootCompleteReceiver _________");
}
}}
My AndroidManifest.xml file as below--
<!-- My Service -->
<service
android:name=".Service.MyService"
android:exported="false"
android:stopWithTask="false" />
<!-- My Broadcast Receiver -->
<receiver
android:name=".Service.MyBroadCastReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.myapp.startservice" />
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="android.intent.action.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</receiver>
And my MainActivity.java file code to start the service ---
public class MainActivity extends AppCompatActivity
{
Button btnStopService;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnStopService = findViewById(R.id.btnStopService);
//get FirebaseToken
getToken();
//start Service
startService();
btnStopService.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyService.class);
stopService(intent);
}
});
}
private void getToken()
{
FirebaseId firebaseId=new FirebaseId();
String token_firebase=firebaseId.getFireBaseToken();
}
private void startService()
{
Intent myIntent = new Intent(this, MyService.class);
PendingIntent pendingIntent = PendingIntent.getService(this, 0, myIntent, 0);
Log.e("TAG", "++++++++++222222++++++++");
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
// calendar.setTimeInMillis(System.currentTimeMillis());
//calendar.add(Calendar.SECOND, 10);
if (alarmManager != null) {
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
Toast.makeText(this, "Start Alarm", Toast.LENGTH_LONG).show();
}
private void s()
{
Intent intent = new Intent(this, MyService.class);
startService(intent);
}}
This is an age old story with custom OS providers like MIUI, Vivo etc
They're very strict with their battery optimization policies and hence they won't allow even sticky services to restart when an app is closed and this is the primary reason why you're facing this issue.
Although there is nothing that you can do from your code that can help your users here but you can take them to their Security Center and ask them to enable auto-start feature.
To do this you have to add this piece of code:
try {
Intent intent = new Intent();
String manufacturer = android.os.Build.MANUFACTURER;
if ("xiaomi".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.miui.securitycenter", "com.miui.permcenter.autostart.AutoStartManagementActivity"));
} else if ("oppo".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.coloros.safecenter", "com.coloros.safecenter.permission.startup.StartupAppListActivity"));
} else if ("vivo".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.vivo.permissionmanager", "com.vivo.permissionmanager.activity.BgStartUpManagerActivity"));
} else if("oneplus".equalsIgnoreCase(manufacturer)) {
intent.setComponent(new ComponentName("com.oneplus.security", "com.oneplus.security.chainlaunch.view.ChainLaunchAppListAct‌​ivity")); }
List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0) {
context.startActivity(intent);
}
} catch (Exception e) {
Crashlytics.logException(e);
}
this app will take the user to the security center where you have to ask them to enable auto-start feature for your app.
Now apps like whatsapp and instagram don't have such issues but i don't clearly know the reason for this, As i have seen on my devices, auto-start is by default enabled for these apps.