2017-05-08 23 views
0

我嘗試使用Xamarin Form創建應用,該應用可以在Android上接收前景和背景通知。對於前景,它工作正常。我創建了Firebase服務來處理前臺通知。但是,我找不到後臺通知工作的方式。對於Xamarin Android,我無法處理單擊事件的後臺通知

首先,我嘗試更新清單以支持此事件。 enter image description here

其次,我添加一些代碼到我的splashactivity來處理後臺通知。 enter image description here

接下來,我使用以下PHP代碼向Firebase服務器發送通知。 enter image description here

在我的設備上,我可以看到上述通知。 enter image description here

我點擊通知打開我的應用程序。然而,沒有任何事發生,設備日誌並沒有給我任何關於正在發生的事情的想法。

enter image description here

有沒有在我的代碼任何不正確的?

謝謝,

+0

[如何在Firebase中的後臺應用程序中處理通知]的可能重複(http://stackoverflow.com/questions/37711082/how-to-handle-notification-when-app-in-background-in-firebase ) – X3Btel

+1

請勿將圖片用於代碼/文本:https://meta.stackoverflow.com/questions/285551/why-not-to-upload-images-of-code-on-so-when-asking-a-問題 – SushiHangover

+0

@ X3Btel我的問題是關於Xamarin而不是原生Android。有幾件事情完全不同。 –

回答

1

從火力地堡資料爲準:

在背景執行的應用程序 手柄通知消息當你的應用程序是在後臺,Android的指示通知消息到系統托盤。用戶點擊通知會默認打開應用程序啓動器。這包括包含通知和數據負載(以及通知控制檯發送的所有消息)的消息。在這些情況下,通知將傳遞到設備的系統托盤,並且數據有效載荷將按照啓動程序活動的目的附加內容進行傳遞。

我能夠通過與FCM有效載荷發送自定義數據,然後在MainActivity的OnCreate解決這個()方法:

var someValue = Intent.GetStringExtra("YourKey"); 
// if result not null then handle how you like... 

在我的情況,我想將用戶重定向到查看通知。

更新:

還有一種方法可以解決這個問題。

覆蓋OnMessageReceived方法...

[Service] 
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] 
public class MyFirebaseMessagingService : FirebaseMessagingService 
{ 
    public override void OnMessageReceived(RemoteMessage message) 
    { 
     SendNotification(message?.Data); 
    } 

    void SendNotification(IDictionary<string, string> data) 
    { 
     // handle your data that got passed 
    } 
} 

,而不是在你的例子這樣做的,:

{ 
    "noticiation" : { 
     "title" : "Test 456", 
     "text" : "Otra 123123" 
    } 
} 

傳遞數據,而不是:

{ 
    "data" : { 
     "title" : "Test 456", 
     "text" : "Otra 123123" 
    } 
} 

OnMessageReceived現在被稱爲兩個當你的應用程序在後臺在前臺。

請注意,此方法意味着您無法使用Firebase中的控制檯發送消息,因爲據我所知,它在有效內容中包含「通知」。