Firebase notification is not working when activity is in background or not

匿名 (未验证) 提交于 2019-12-03 01:11:01

问题:

I have same coding as provided by firebase sample code. It works fine when activity is in foreground state or opened. But when activity is closed or in background state then it does not work properly. Problem is like that...

  1. App icon is not shown of application.

  2. By default take app name as a title of notification

And when app is in background state or not opened on device that time notification occurs then LogCat shows following details :

09-16 15:55:56.056 12113-12113/com.testdemofb D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization. 09-16 15:55:56.089 12113-12113/com.testdemofb D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization. 09-16 15:55:56.176 12113-12113/com.testdemofb I/FA: App measurement is starting up, version: 9452 09-16 15:55:56.177 12113-12113/com.testdemofb I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE 09-16 15:55:56.286 12113-12113/com.testdemofb I/FirebaseInitProvider: FirebaseApp initialization successful 09-16 15:55:56.518 12113-12147/com.testdemofb I/FA: Tag Manager is not found and thus will not be used 

if any solution for that then help me

Thanks...

MyFirebaseMessagingService.java

package com.testdemofb;  import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; import android.media.RingtoneManager; import android.net.Uri; import android.support.v4.app.NotificationCompat; import android.util.Log; import android.widget.Toast;  import com.google.firebase.messaging.FirebaseMessagingService; import com.google.firebase.messaging.RemoteMessage;  public class MyFirebaseMessagingService extends FirebaseMessagingService {  private static final String TAG = "MyFirebaseMsgService";   @Override public void onMessageReceived(RemoteMessage remoteMessage) {      // Check if message contains a data payload.     if (remoteMessage.getData().size() > 0) {         Log.d(TAG, "Message data payload: " + remoteMessage.getData());         sendNotification(remoteMessage.getData().get("title"));     }      // Check if message contains a notification payload.     if (remoteMessage.getNotification() != null) {         Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody());         Log.d(TAG, "Message Notification BodyLocalize: " + remoteMessage.getNotification().getBodyLocalizationKey());         Log.d(TAG, "Message Notification Title: " + remoteMessage.getNotification().getTitle());         Log.d(TAG, "Message Notification TitleLocalize: " + remoteMessage.getNotification().getTitleLocalizationKey());         sendNotification(remoteMessage.getNotification().getBody());     }   }  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);      Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);     Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)             .setLargeIcon(bm)             .setSmallIcon(R.mipmap.ic_launcher)             .setContentTitle("Title")             .setContentText(messageBody)             .setAutoCancel(true)             .setSound(defaultSoundUri)             .setContentIntent(pendingIntent);      NotificationManager notificationManager =             (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);      notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());  } } 

AndroidManifest.xml file put like this between application tag...

    <service android:name=".MyFirebaseMessagingService"         android:process="remote">         <intent-filter>             <action android:name="com.google.firebase.MESSAGING_EVENT" />         </intent-filter>     </service>      <service android:name=".MyFirebaseInstanceIDService" >         <intent-filter>             <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />         </intent-filter>     </service> 

回答1:

There are two types of messages that FCM uses, viz. notification and data. Read more about it here. I think you are sending a notification. You need to send a data message.

Something like this.

{ "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",    "data" : {  "Nick" : "Mario",  "body" : "great match!",  "Room" : "PortugalVSDenmark" }, } 


回答2:

Send notification from Firebase console : Works for both background and foreground app

Instead of onMessageReceived, override zzm() of FirebaseMessagingService and create your custom notification from here

@Override public void zzm(Intent intent) {     Log.e(TAG, "zzm : " + intent);     createNotification();         }  @Override public void onMessageReceived(RemoteMessage remoteMessage) {  } 


回答3:



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!