0

I know that it have a lot of questions about this theme, but no one works in my project, and I don't know why.

I'm using Firebase, if my app is OPEN or in BACKGROUND, his receive the message, but when I kill my app don't appear more.

Using Android 8.0

My codes: AndroidManifest.xml

 <meta-data
 android:name="com.google.firebase.messaging.default_notification_icon"
 android:resource="@drawable/default_notification_icon" />
 <meta-data
 android:name="com.google.firebase.messaging.default_notification_color"
 android:resource="@color/colorPrimary" />
 <meta-data android:name="com.google.firebase.messaging.default_notification_channel_id"
 android:value="@string/default_notification_channel_id" />
 <service
 android:name=".service.PineFirebaseMessagingService"
 android:enabled="true"
 android:exported="true"
 android:permission="com.google.android.c2dm.permission.SEND">
 <intent-filter>
 <action android:name="com.google.firebase.MESSAGING_EVENT" />
 <action android:name="com.google.android.c2dm.intent.RECEIVE" />
 </intent-filter>
 </service>
 <service
 android:name=".service.PineFirebaseInstanceIDService"
 android:enabled="true"
 android:exported="true">
 <intent-filter>
 <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
 </intent-filter>
 </service>

FirebaseMessagingService.java

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
 channelId = getString(R.string.default_notification_channel_id);
 Log.i("Msg", "Message received [" + remoteMessage.getData() + "]");
 Intent intent = new Intent(this, HomeActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 PendingIntent pendingIntent = PendingIntent.getActivity(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
 Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
 NotificationCompat.Builder notificationBuilder = new
 NotificationCompat.Builder(this, channelId)
 .setSmallIcon(R.drawable.default_notification_icon)
 .setColor(ContextCompat.getColor(this, R.color.colorPrimary))
 .setContentTitle(remoteMessage.getData().get("title"))
 .setContentText(remoteMessage.getData().get("message"))
 .setAutoCancel(true)
 .setContentIntent(pendingIntent)
 .setSound(defaultSoundUri);
 NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
 NotificationChannel mChannel = new NotificationChannel(
 channelId,
 remoteMessage.getData().get("title"),
 NotificationManager.IMPORTANCE_HIGH);
 mChannel.setDescription(remoteMessage.getData().get("message"));
 mChannel.enableLights(true);
 mChannel.enableVibration(true);
 mChannel.setLightColor(Color.BLUE);
 notificationManager.createNotificationChannel(mChannel);
 notificationManager.cancel(id);
 }
 notificationManager.notify(id, notificationBuilder.build());
}

FirebaseInstanceIdService.java

@Override
public void onTokenRefresh() {
 super.onTokenRefresh();
 String refreshedToken = FirebaseInstanceId.getInstance().getToken();
 Log.d("Token", "Refreshed token: " + refreshedToken);
}

Code in C# to send the push

async Task SendPushAndroidAsync(Device device, InArgumentRequest push, Notification notification)
 {
 var applicationID = configuration["Push:Google:ApplicationKey"];
 var data1 = new
 {
 to = DeviceIdEnviroment(device.Token, true),
 android = new {
 priority = "normal"
 },
 data = new
 {
 notificationId = notification.Id,
 title = notification.Title,
 message = notification.Message,
 type = notification.Type,
 date = notification.DateCreated.ToString("dd/MM/yyyy HH:mm"),
 android_channel_id = "channelidandroid"
 }
 };
 try
 {
 var jsonBody = JsonConvert.SerializeObject(data1);
 using (var httpRequest = new HttpRequestMessage(HttpMethod.Post, "https://fcm.googleapis.com/fcm/send"))
 {
 httpRequest.Headers.TryAddWithoutValidation("Authorization", "key=" + applicationID);
 httpRequest.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");
 using (var httpClient = new HttpClient())
 {
 var result = await httpClient.SendAsync(httpRequest);
 if (result.IsSuccessStatusCode)
 {
 await UpdateStatusNotification(notification, true);
 }
 else
 {
 await UpdateStatusNotification(notification, false, result.ReasonPhrase);
 }
 }
 }
 }
 catch (Exception ex)
 {
 string error = ex.InnerException.Message;
 await UpdateStatusNotification(notification, false, error);
 }
 }​

Thanks!

asked Jun 15, 2018 at 20:22
2
  • which phone are you using ? is it oneplus 5t Commented Jun 15, 2018 at 20:27
  • @nishant i'm using Asus Zenphone 4, but in Samsung S9 don't work too Commented Jun 15, 2018 at 20:30

2 Answers 2

1

you need to go to settings> battery optimization> and disable it for your app. as that stops your application from running background services

answered Jun 15, 2018 at 20:32
Sign up to request clarification or add additional context in comments.

3 Comments

but i want to receive the push notification when the app is killed :/, thanks for your quickly answer
you will get push notification when app is killed . by disabling the battery optimization from your app in settings
i tried this, and don't work yet, only on android 7.1 works fine
0

The message priority should not be nested inside android and should have value "high":

 var data1 = new
 {
 to = DeviceIdEnviroment(device.Token, true),
 priority = "high",
 data = new
 {
 notificationId = notification.Id,
 title = notification.Title,
 message = notification.Message,
 type = notification.Type,
 date = notification.DateCreated.ToString("dd/MM/yyyy HH:mm"),
 android_channel_id = "channelidandroid"
 }
 };

It's possible you are not receiving the message because the device is in Doze Mode or the app is in App Standby Mode. This is explained in the documentation.

After making the change to your message you can confirm that it is received with high priortity by calling RemoteMessage.getOriginalPriority().

answered Jun 15, 2018 at 20:56

5 Comments

I tried the both ways and don't work yet, in two different phones and in one that have Android 7.1, this phone works fine
I'm not familiar with C#. It looks like when you create your message, you are declaring the priority property in an android property. It should be at the same level as to and data. I updated my answer to show that.
I tried this too and don't works yet :/ i'm started to think to have a error in Android 8.0 for this case, because nothing work properly :(
Have you tried composing and sending a data message using the Firebase console? Under "Advanced Options" you can set the data fields and priority. I am able to receive high-priority data messages on Android 8 device after app is killed.
Yes I tried this now and don't work too... I think have something in FCM with Android 8.0 that blocks the services, because when we kill the application the all services is killed too in Android 8.0, if I do this with an Android 7.1 works well.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.