0

im referring this answer for foreground and background to receive firebase notification --> https://stackoverflow.com/a/38451582/12553303

actually following is my doubt:----------

what if i didnt code for foreground condition(talking about push notification) still i will get notifcation when my app is in background right????-->yes

but when im on foreground state and i pushed a notification from firebase -->i wont see notification on status bar that is also okay(beacuse suppose i didnt override onmessagereceive() method)....then next i go to background state i didnt see any notification though which i sent for background

what should i do to get notification which i sent for background like going from foreground state(with no method of onmessagereceived()) to background state??*

thanks need advice and clarification on this ...

is this even possible get that notification moving from foreground state to background state??

code :----

override fun onMessageReceived(p0: RemoteMessage) {
    super.onMessageReceived(p0)
    Log.d("msg", "onMessageReceived: " + p0.getData().get("message"))
    val intent = Intent(this, OrderListActivity::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
    val channelId = "Default"
    val builder: NotificationCompat.Builder =NotificationCompat.Builder(this, channelId)
        .setSmallIcon(R.mipmap.ic_launcher)
        .setContentTitle(p0.getNotification()?.getTitle())
        .setContentText(p0.getNotification()?.getBody()).setAutoCancel(true)
        .setContentIntent(pendingIntent)
    val manager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val channel = NotificationChannel(channelId,
            "Default channel",
            NotificationManager.IMPORTANCE_DEFAULT)
        manager.createNotificationChannel(channel)
    }
    manager.notify(0, builder.build())

}

any advice will be appreciated thanks

Wini
  • 1,906
  • 1
  • 12
  • 31

1 Answers1

0

To receive messages in an Android app, Firebase Notifications contains different mechanisms when app is foreground or in background.

When the app is closed, your notifications are processed by the Google Service process, which display your notifications as required, including click action i.e.opening the app and the notification icon.

When the app is in foreground, the received messages are processed by the app.

To fix this, following are the steps to follow:-

1.Create a new class that extends FirebaseMessagingService 2.Implement the onMessageReceived() method 3.Then get the Notification object from the remoteMessage and create your own Android Notification like,

public class MyNotificationService extends FirebaseMessagingService { 
  @Override
  public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    Notification notification = new NotificationCompat.Builder(this)
      .setContentTitle(remoteMessage.getNotification().getTitle())
      .setContentText(remoteMessage.getNotification().getBody())
      .setSmallIcon(R.mipmap.ic_launcher)
      .build();
     NotificationManagerCompat manager = NotificationManagerCompat.from(getApplicationContext());
     manager.notify(1, notification);
   }
}

4.Then add the Service in your AndroidManifest.xml

<service android:name=”.MyNotificationService”>
   <intent-filter>
      <action android:name=”com.google.firebase.MESSAGING_EVENT”/>
   </intent-filter>
</service>
Jyoti
  • 51
  • 2
  • what if i didnt added `onmessageReceived` in my code ...suppose my app is in background i will get notification but when my app is in foreground notification didnt come (well thats okay because i didnt override that method) when i go back to background i didnt received any notification ....where did that notification went ? will it never received? – Wini Dec 11 '20 at 04:58