2017-04-12 143 views
0

當我的應用程序正在運行(在前臺和後臺),我可以收到通知,並使用Android的NotificationManager顯示通知給用戶。但是,在將應用程序從最近的應用程序中移開後,當應用程序收到通知時,我收到消息Unfortunately, MyApp has stopped workingGCM通知應用程序崩潰後,我關閉應用程序

我知道應用程序應該能夠在從最近一次刷卡後收到通知,因爲我已經設置了百度通知,並且能夠在刷開應用程序後接收它們。但是使用GCM,它只會導致應用程序崩潰。這是有問題的代碼:

[BroadcastReceiver(Name = "com.my.pkg.MyGcmReceiver", Exported = true, Permission = "com.google.android.c2dm.permission.SEND")] 
[IntentFilter(new string[] { "com.google.android.c2dm.intent.RECEIVE" }, Categories = new string[] { "com.my.pkg" })] 
public class MyGcmReceiver : GcmReceiver 
{ 
    public override void OnReceive(Context context, Intent intent) 
    { 
     base.OnReceive(context, intent); 
     System.Diagnostics.Debug.WriteLine("MyGcmReceiver - OnReceive called &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); 
     CreateNotification(); 
    } 

    private void CreateNotification() 
    { 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(Xamarin.Forms.Forms.Context); 

     builder.SetAutoCancel(true); 
     builder.SetDefaults(NotificationCompat.DefaultAll); 
     builder.SetSmallIcon(Resource.Drawable.icon); 
     builder.SetContentTitle("HI"); 
     builder.SetContentText("MSG FROM GCM"); 
     builder.SetPriority((int)NotificationPriority.Max); 

     NotificationManager manager = (NotificationManager)Xamarin.Forms.Forms.Context.GetSystemService(Context.NotificationService); 

     manager.Notify(0, builder.Build()); 
    } 
} 

通過在OnReceive方法註釋掉線CreateNotification();,應用程序不會崩潰,當然那不會產生,並顯示該通知。

+0

您使用Google雲消息傳遞而不是Firebase可以傳遞消息的任何原因? –

+0

@MadhavShenoy我寫的是一樣的。 –

+1

:d隨着FCM你不需要編寫代碼來處理通知 –

回答

1

就像我的評論稱,使用FCM似乎輕鬆了許多,因爲它需要處理的信息服務。

我使用Visual Studio 2017年和Xamarin窗體版本2.3.224(還應該有2.3.3.193工作)。我還在我的解決方案中附加了Nuget軟件包的屏幕截圖。我將Xamarin.Firebase.Messaging添加到了Android項目中,而不是表單項目。 enter image description here

要運行FCM,你必須創建一個帳戶,並下載谷歌-services.json文件。更多關於此here

要Xamarin形式實現這個我在Android中創建2個服務, 1依存服務 2意圖服務。

依賴項服務只檢查PlayService是否可用並獲取FirebaseToken。

public class PlayServices: IPlayService 
{ 
    public string IsPlayServicesAvailable() 
    { 
     int resultCode = GoogleApiAvailability.Instance.IsGooglePlayServicesAvailable(Forms.Context); 
     if (resultCode != ConnectionResult.Success) 
     { 
      if (GoogleApiAvailability.Instance.IsUserResolvableError(resultCode)) 
       return GoogleApiAvailability.Instance.GetErrorString(resultCode); 
      return "This device is not supported"; 
     } 
     return "Available"; 
    } 

    public string FirebaseToken => FirebaseInstanceId.Instance.Token; 
} 

現在,w.r.t.在IntentService

[Service] 
[IntentFilter(new[] { "com.google.firebase.INSTANCE_ID_EVENT" })] 
public class MyFirebaseIidService : FirebaseInstanceIdService 
{ 
    const string TAG = "MyFirebaseIIDService"; 
    public override void OnTokenRefresh() 
    { 
     var refreshedToken = FirebaseInstanceId.Instance.Token; 
     Log.Debug(TAG, "Refreshed token: " + refreshedToken); 
     SendRegistrationToServer(refreshedToken); 
    } 
    void SendRegistrationToServer(string token) 
    { 
     // Add custom implementation, as needed. 
    } 

} 

爲應用程序啓動時這將立即運行和應用程序啓動後,在約6-7秒內完成一般。這會從Firebase生成一個令牌並將其提供給應用。

儘管你不需要令牌(因爲你可以發送推送消息到安裝應用程式的所有手機),你可以用它發送推送通知只是僅此電話。

希望這會有所幫助

+0

Xamarin.Firebase.Messaging軟件包的版本是什麼?我試圖將最新的穩定版本42.1001.0安裝到android項目中,但它表明依賴軟件包未能安裝,當我編譯項目時,有20個警告和9個錯誤。 –

+0

有趣的是,我清理解決方案並重建它,現在工作正常...然而,每次我嘗試將消息傳遞包安裝到新的表單項目時,都會發生相同的錯誤,並且我從來沒有嘗試清理並重建它,這就是爲什麼我認爲FCM不能用於Xamarin形式......無論如何感謝。 –