11

我正在檢查Firebase Cloud Messaging以發送通知。當應用程序處於打開狀態時,它已經實現它並接收通知。但是,如果我關閉應用程序,它不再發出通知。有沒有解決方案。Android應用程序在關閉時未收到FCM通知

代碼:

WebRequest wRequest; 
wRequest = WebRequest.Create("https://fcm.googleapis.com/fcm/send"); 
wRequest.Method = "post"; 
wRequest.ContentType = " application/json;charset=UTF-8"; 
wRequest.Headers.Add(string.Format("Authorization: key={0}", AppId)); 

wRequest.Headers.Add(string.Format("Sender: id={0}", SenderId)); 

string postData = "{\"registration_ids\":[\"" + regIds + "\"], \"data\": "+ value +"}"; 

Byte[] bytes = Encoding.UTF8.GetBytes(postData); 
wRequest.ContentLength = bytes.Length; 

Stream stream = wRequest.GetRequestStream(); 
stream.Write(bytes, 0, bytes.Length); 
stream.Close(); 

WebResponse wResponse = wRequest.GetResponse(); 

消息服務 -

public class MessagingService extends FirebaseMessagingService { 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Map<String, String> data = remoteMessage.getData(); 
     sendNotification(data); 
    } 

    public void showMessage(Map<String, String> serverData) { 
     Intent i = new Intent(this,MainActivity.class); 
     i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

     PendingIntent pendingIntent = PendingIntent.getActivity(this,0,i,PendingIntent.FLAG_UPDATE_CURRENT); 

     NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
       .setAutoCancel(true) 
       .setContentTitle(serverData.get("Title")) 
       .setContentText(serverData.get("Message")) 
       .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) 
       .setContentIntent(pendingIntent); 

     NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

     manager.notify(Integer.parseInt(serverData.get("ItemId")),builder.build()); 
    } 

    private void sendNotification(Map<String, String> serverData) { 
     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_UPDATE_CURRENT); 

     long[] pattern = {500,500,500,500,500}; 

     Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this) 
       .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) 
       .setContentTitle(serverData.get("Title")) 
       .setContentText(serverData.get("Message")) 
       .setAutoCancel(true) 
       .setVibrate(pattern) 
       .setLights(Color.BLUE,1,1) 
       .setSound(defaultSoundUri) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(Integer.parseInt(serverData.get("ItemId")), notificationBuilder.build()); 
    } 

} 

主要活動 -

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     FirebaseMessaging.getInstance().subscribeToTopic("test"); 
     FirebaseInstanceId.getInstance().getToken(); 
    } 
} 

Manifest-

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="test.com.firebasenotify"> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.READ_PHONE_STATE" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <service android:name=".MessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
      </intent-filter> 
     </service> 
     <service android:name=".InstanceIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
      </intent-filter> 
     </service> 

    </application> 

</manifest> 
+1

顯示FCM監聽器類,並顯示POST請求您對FCM –

+0

更新消息服務,FCM職位和主要活動代碼 –

+0

你在的onReceive收到消息至少? –

回答

8

代碼沒有問題。我正在使用Mi note 4,並且在應用程序關閉時它不會在Mi4中顯示通知。我測試了與其他Android設備和它的工作正常。

感謝Tim Castelijns和Frank van Puffelen參與對話。

+2

對於MI,您需要將應用程序置於安全模式下的自動啓動模式才能在應用程序關閉時收到通知 –

+0

感謝man,redmi prime2也得到同樣的問題。您是否在MI設備中發現了此問題的任何解決方案? – sandeepmaaram

-1

添加在POST有效載荷將解決這一參數的這個problem.The值必須是從0到2419200秒的持續時間time_to_live鍵,它對應於用於該FCM存儲並嘗試傳遞的最大時間週期消息。 「time_to_live」:3Refer Firebase Docs

+1

只有當用戶回到應用程序時,這可能是有效的。如果您看到代碼中沒有通知對象,則爲 – ioanb7

-3

作爲每FCM文檔onMessageReceived()時的應用程序是在背景將不被調用。你應該在發送順序通知對象,以顯示它在系統托盤中,當用戶點擊它發射活動將開放數據處理爲額外意圖。對於有效載荷,您應該使用數據對象。 see docsReceive Messages in an Android App

+2

。他只發送數據對象 –

+0

您還需要發送通知對象。 – Sjd

+2

但是如果我們將發送通知對象,那麼它將不會觸及onMessageReceived,並且通知將由android本身處理,並將顯示在通知托盤(沒有您的客戶端應用程序)中。如果我們發送沒有通知對象,但有數據對象,那麼它總是會觸發onMessageReceived。 –

相關問題