Reputation: 925
When i'm sending message through Firebase Console and my application is in the background or killed everything works, notification shows up, but when my app is in foreground and then i send message, i get
I use emulator with android 8.0 image
Also, when System UI error appears, my message gets logged with everything i send there, there's just this error and nothing about it in the logs.
My code here:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d("FirebaseMessage", "FROM: " + remoteMessage.getFrom());
if(remoteMessage.getData().size() > 0) {
Log.d("FirebaseMessage", "Message Data: " + remoteMessage.getData());
}
if(remoteMessage.getNotification() != null) {
Log.d("Firebae Message", "Message body: " +remoteMessage.getNotification().getBody());
sendNotification(remoteMessage.getNotification().getBody());
}
}
private void sendNotification(String body) {
Intent intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String CHANNEL_ID = "test_channel";
CharSequence name = "testChannel";
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("[TEST] Message")
.setContentText(body)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(mChannel);
notificationManager.notify(0, notificationBuilder.build());
}
}
}
EDIT: That's what appear in logs after crash:
05-04 17:42:40.889 7028-7102/com.example.wiktor.notesapptesting D/FirebaseMessage: FROM: 632610386607
05-04 17:42:40.896 7028-7102/com.example.wiktor.notesapptesting D/Firebae Message: Message body: test
05-04 17:42:40.919 7028-7055/com.example.wiktor.notesapptesting D/FA: Logging event (FE): notification_receive(_nr), Bundle[{firebase_event_origin(_o)=fcm, firebase_screen_class(_sc)=PlusUsersMainActivity, firebase_screen_id(_si)=4052769648448254679, message_device_time(_ndt)=0, message_time(_nmt)=1525455768, message_id(_nmid)=3256029686705242789}]
05-04 17:42:40.982 7028-7055/com.example.wiktor.notesapptesting V/FA: Connecting to remote service
05-04 17:42:41.021 7028-7055/com.example.wiktor.notesapptesting D/FA: Logging event (FE): notification_foreground(_nf), Bundle[{firebase_event_origin(_o)=fcm, firebase_screen_class(_sc)=PlusUsersMainActivity, firebase_screen_id(_si)=4052769648448254679, message_device_time(_ndt)=0, message_time(_nmt)=1525455768, message_id(_nmid)=3256029686705242789}]
05-04 17:42:41.081 7028-7055/com.example.wiktor.notesapptesting V/FA: Connection attempt already in progress
05-04 17:42:41.081 7028-7055/com.example.wiktor.notesapptesting D/FA: Connected to remote service
05-04 17:42:41.082 7028-7055/com.example.wiktor.notesapptesting V/FA: Processing queued up service tasks: 2
05-04 17:42:43.142 7028-7033/com.example.wiktor.notesapptesting I/zygote: Do partial code cache collection, code=124KB, data=66KB
After code cache collection, code=124KB, data=66KB
Increasing code cache capacity to 512KB
05-04 17:42:46.155 7028-7055/com.example.wiktor.notesapptesting V/FA: Inactivity, disconnecting from the service
Upvotes: 2
Views: 789
Reputation: 925
Ok guys, I found the solution.
So, the problem was this line of code .setSmallIcon(R.mipmap.ic_launcher)
I don't really know why, but when i change that to something else, in my case to drawable
icon .setSmallIcon(R.drawable.friend_main_icon)
it works, app is not crashing anymore.
Upvotes: 4