1

我擁有與Firebase示例代碼提供的代碼相同的代碼。當活動處於前景狀態或打開狀態時,它可以正常工作。但是,當活動關閉或在後臺狀態下,它不能正常工作。問題就是這樣...當活動處於後臺時,Firebase通知不起作用

  1. 應用程序圖標未顯示應用程序。

  2. 默認情況下采取的應用程序名稱的通知標題

當應用程序在後臺狀態或當時通知時再logcat的設備沒有打開顯示以下信息:

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 

如果有任何解決方案,然後幫助我

謝謝...

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文件把這樣應用之間標籤...

<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> 
+0

你能分享你的執行生成notofication –

+0

分享我的代碼生成通知@ RaguSwaminathan –

+0

你檢查是否調用sendnotification方法嗎? ?嘗試在那裏記錄和檢查 –

回答

2

有兩種類型的FCM使用的消息,即。通知和數據。閱讀更多關於它here。我認爲你正在發送通知。您需要發送數據信息。

就是這樣。從火力地堡控制檯

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

發送通知:同時適用於背景和前景應用

相反onMessageReceived的,覆蓋ZZM()FirebaseMessagingService,並從這裏創建自定義通知

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

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 

} 
+0

zzm方法到底做了什麼?它是好的做法重寫它? – Vlad

-2

我想FireBase是這樣工作的...

enter image description here

+0

https://firebase.google.com/docs/cloud-messaging/android/receive –

相關問題