1

当我的应用程序关闭时,我收到来自FCM的新通知,但是当我点击它时,我的应用程序将崩溃。当我的应用程序处于后台或前台时,它可以,然后他们将打开通知的正确页面。我不打电话给SendNotification,因为我不想在应用程序处于前台时发出本地通知。如何处理通过FCM关闭应用程序时的通知?

我FCM服务:

[Service] 
[IntentFilter(new[] { "com.google.firebase.MESSAGING_EVENT" })] 
public class MyFirebaseMessagingService : FirebaseMessagingService 
{ 
    const string TAG = "MyFirebaseMsgService"; 
    public override void OnMessageReceived(RemoteMessage message) 
    { 
     Log.Debug(TAG, "From: " + message.From); 
     MessagingCenter.Send(App.CurrentApp, "ReceivedNotification"); 
    } 

    void SendNotification(string messageBody) 
    { 
     var intent = new Intent(this, typeof(MainActivity)); 
     intent.AddFlags(ActivityFlags.ClearTop); 
     var pendingIntent = PendingIntent.GetActivity(this, 0, intent, PendingIntentFlags.OneShot); 

     var notificationBuilder = new Android.App.Notification.Builder(this) 
      .SetSmallIcon(Resource.Drawable.ic_launcher) 
      .SetContentTitle("FCM Message") 
      .SetContentText(messageBody) 
      .SetAutoCancel(true) 
      .SetContentIntent(pendingIntent); 

     var notificationManager = NotificationManager.FromContext(this); 
     notificationManager.Notify(0, notificationBuilder.Build()); 
    } 
} 

通知中的OnCreate处理:

protected override void OnCreate(Bundle bundle) 
    { 


     TabLayoutResource = Resource.Layout.Tabbar; 
     ToolbarResource = Resource.Layout.Toolbar; 

     base.OnCreate(bundle); 
     Forms.Init(this, bundle); 
     ImageCircleRenderer.Init(); 

     IsPlayServicesAvailable(); 

     if (Intent.Extras != null) 
     { 
      NotificationSendObject notifcation = new NotificationSendObject(); 
      foreach (var key in Intent.Extras.KeySet()) 
      { 
       switch (key) 
       { 
        case "object_id": 
         notifcation.objectId = Intent.Extras.GetString(key); 
         break; 
        case "notification_id": 
         notifcation.notificationId = Intent.Extras.GetString(key); 
         break; 
        case "object_type": 
         notifcation.objectType = Intent.Extras.GetString(key); 
         break; 
       } 
      } 
      if (notifcation.notificationId != null) 
      { 
       MessagingCenter.Send(App.CurrentApp, "OnTapNotification", notifcation); 
      } 
     } 


     SecureStorageImplementation.StoragePassword = "*****"; 

     LoadApplication(new App()); 

    } 
+0

把你的崩溃日志在这里。 –

+0

OnCreate()用大写的O?你确定吗? –

+0

嗨。发布堆栈跟踪。 :) –

回答

1

我找到一个解决方案,该Intent.Extras必须LoadApplication方法后调用。

修正:

protected override void OnCreate(Bundle bundle) 
{ 


    TabLayoutResource = Resource.Layout.Tabbar; 
    ToolbarResource = Resource.Layout.Toolbar; 

    base.OnCreate(bundle); 
    Forms.Init(this, bundle); 
    ImageCircleRenderer.Init(); 

    IsPlayServicesAvailable(); 

    SecureStorageImplementation.StoragePassword = "*****"; 

    LoadApplication(new App()); 

    if (Intent.Extras != null) 
    { 
     NotificationSendObject notifcation = new NotificationSendObject(); 
     foreach (var key in Intent.Extras.KeySet()) 
     { 
      switch (key) 
      { 
       case "object_id": 
        notifcation.objectId = Intent.Extras.GetString(key); 
        break; 
       case "notification_id": 
        notifcation.notificationId = Intent.Extras.GetString(key); 
        break; 
       case "object_type": 
        notifcation.objectType = Intent.Extras.GetString(key); 
        break; 
      } 
     } 
     if (notifcation.notificationId != null) 
     { 
      MessagingCenter.Send(App.CurrentApp, "OnTapNotification", notifcation); 
     } 
    } 

} 
相关问题