当iOS应用程序处于后台时,无法接收FCM推送通知
目前我正在开发一个xamarin ios应用程序,我正在尝试使用firebase云消息传递实现推送通知,我已经更新了info.plist、entity.plist 还添加了GoogleService-Info.plist,即 下面是我在AppDelegate.cs中所做的工作当iOS应用程序处于后台时,无法接收FCM推送通知,ios,xamarin.forms,xamarin.ios,firebase-cloud-messaging,Ios,Xamarin.forms,Xamarin.ios,Firebase Cloud Messaging,目前我正在开发一个xamarin ios应用程序,我正在尝试使用firebase云消息传递实现推送通知,我已经更新了info.plist、entity.plist 还添加了GoogleService-Info.plist,即 下面是我在AppDelegate.cs中所做的工作 using Foundation; using UIKit; using UserNotifications; using Firebase.Core; using Firebase.CloudMessaging;
using Foundation;
using UIKit;
using UserNotifications;
using Firebase.Core;
using Firebase.CloudMessaging;
using System;
using TrendSurveyApp.iOS;
using Newtonsoft.Json;
using System.Collections.Generic;
using AudioToolbox;
namespace CloudMessagingSample
{
// The UIApplicationDelegate for the application. This class is responsible for launching the
// User Interface of the application, as well as listening (and optionally responding) to application events from iOS.
[Register("AppDelegate")]
public class AppDelegate : UIApplicationDelegate, IUNUserNotificationCenterDelegate, IMessagingDelegate
{
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
// Override point for customization after application launch.
// If not required for your application you can safely delete this method
//(Window.RootViewController as UINavigationController).PushViewController(new DialogViewController1(this), true);
App.Configure();
Messaging.SharedInstance.Delegate = this;
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
UNUserNotificationCenter.Current.Delegate = this;
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
(granted, error) =>
{
if (granted)
{
InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemoteNotifications);
}
});
}
else if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
{
var pushSettings = UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
new NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(pushSettings);
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
else
{
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
}
// To connect with FCM. FCM manages the connection, closing it
// when your app goes into the background and reopening it
// whenever the app is foregrounded.
Messaging.SharedInstance.ShouldEstablishDirectChannel = true;
return true;
}
public void DidReceiveRegistrationToken(Messaging messaging, string fcmToken)
{
// Monitor token generation: To be notified whenever the token is updated.
LogInformation(nameof(DidReceiveRegistrationToken), $"Firebase registration token: {fcmToken}");
// TODO: If necessary send token to application server.
// Note: This callback is fired at each app startup and whenever a new token is generated.
}
// You'll need this method if you set "FirebaseAppDelegateProxyEnabled": NO in GoogleService-Info.plist
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
Messaging.SharedInstance.ApnsToken = deviceToken;
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
Messaging.SharedInstance.AppDidReceiveMessage(userInfo);
LogInformation(nameof(DidReceiveRemoteNotification), userInfo);
completionHandler(UIBackgroundFetchResult.NewData);
var myData = JsonConvert.DeserializeObject<List<string>>(userInfo[new NSString("webContentList")] as NSString);
}
[Export("messaging:didReceiveMessage:")]
public void DidReceiveMessage(Messaging messaging, RemoteMessage remoteMessage)
{
// Handle Data messages for iOS 10 and above.
HandleMessage(remoteMessage.AppData);
var fcmToken = Messaging.SharedInstance.FcmToken;
LogInformation(nameof(DidReceiveMessage), remoteMessage.AppData);
}
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
SystemSound.Vibrate.PlayAlertSound();
SystemSound.Vibrate.PlaySystemSound();
completionHandler(UNNotificationPresentationOptions.Alert);
}
void HandleMessage(NSDictionary message)
{
if (message == null)
return;
MessageType messageType;
if (message.ContainsKey(new NSString("aps")))
messageType = MessageType.Notification;
else
messageType = MessageType.Data;
var e = new UserInfoEventArgs(message, messageType);
}
void LogInformation(string methodName, object information) => Console.WriteLine($"\nMethod name: {methodName}\nInformation: {information}");
}
}
<代码>使用基础;
使用UIKit;
使用用户通知;
使用Firebase.Core;
使用Firebase.CloudMessaging;
使用制度;
使用TrendSurveyApp.iOS;
使用Newtonsoft.Json;
使用System.Collections.Generic;
使用音频工具箱;
命名空间CloudMessagingSample
{
//应用程序的UIApplicationLegate。此类负责启动
//应用程序的用户界面,以及侦听(和可选地响应)来自iOS的应用程序事件。
[注册(“AppDelegate”)]
公共类AppDelegate:UIApplicationDelegate、IUNUserNotificationCenterDelegate、IMessagingDelegate
{
公共覆盖bool FinishedLaunching(UIApplication应用程序、NSDictionary启动选项)
{
//应用程序启动后自定义的覆盖点。
//如果应用程序不需要,您可以安全地删除此方法
//(Window.RootViewController作为UINavigationController)。PushViewController(新对话框viewcontroller1(this),true);
App.Configure();
Messaging.SharedInstance.Delegate=此;
UIRemoteNotificationType notificationTypes=UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
if(UIDevice.CurrentDevice.CheckSystemVersion(10,0))
{
UNUserNotificationCenter.Current.Delegate=此;
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound,
(已批准,错误)=>
{
如果(授予)
{
InvokeOnMainThread(UIApplication.SharedApplication.RegisterForRemotonifications);
}
});
}
else if(UIDevice.CurrentDevice.CheckSystemVersion(8,0))
{
var pushSettings=UIUserNotificationSettings.GetSettingsForTypes(
UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound,
新NSSet());
UIApplication.SharedApplication.RegisterUserNotificationSettings(推送设置);
UIApplication.SharedApplication.RegisterForRemotonifications();
}
其他的
{
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
}
//连接FCM。FCM管理连接,关闭连接
//当你的应用程序进入后台并重新打开时
//每当应用程序被前景化时。
Messaging.SharedInstance.ShouldStablishDirectChannel=true;
返回true;
}
public void DidReceiveRegistrationToken(消息传递,字符串fcmToken)
{
//监视令牌生成:在令牌更新时通知。
登录信息(姓名(DidReceiverRegistrationToken),$“Firebase注册令牌:{fcmToken}”);
//TODO:如有必要,将令牌发送到应用程序服务器。
//注意:每次应用程序启动时以及每当生成新令牌时,都会触发此回调。
}
//如果在GoogleService-Info.plist中设置“FirebaseAppDelegateProxyEnabled”:否,则需要此方法
公共覆盖无效注册更正(UIApplication应用程序,NSData deviceToken)
{
Messaging.SharedInstance.ApnsToken=deviceToken;
}
public override void DidReceiveEmotentification(UIApplication应用程序、NSDictionary userInfo、Action completionHandler)
{
Messaging.SharedInstance.AppDidReceiveMessage(userInfo);
登录信息(姓名(DidReceiveMemoteNotification)、用户信息);
completionHandler(UIBackgroundFetchResult.NewData);
var myData=JsonConvert.DeserializeObject(userInfo[new NSString(“webContentList”)]作为NSString);
}
[导出(“消息传递:didReceiveMessage:”)]
public void DidReceiveMessage(消息传递、远程消息、远程消息)
{
//处理iOS 10及以上版本的数据消息。
HandleMessage(remoteMessage.AppData);
var fcmToken=Messaging.SharedInstance.fcmToken;
登录信息(名称(DidReceiveMessage)、remoteMessage.AppData);
}
[导出(“userNotificationCenter:willPresentNotification:withCompletionHandler:”)]
public void WillPresentNotification(未使用通知中心、未通知通知通知、操作完成处理程序)
{
SystemSound.Vibrate.PlayAlertSound();
SystemSound.Vibrate.PlaySystemSound();
completionHandler(UNNotificationPresentationOptions.Alert);
}
无效HandleMessage(NSDictionary消息)
{
如果(消息==null)
返回;
MessageType消息类型;
if(message.ContainsKey(新的NSString(“aps”))
messageType=messageType.Notification;
其他的
messageType=messageType.Data;
var e=新的UserInfoEventArgs(消息,消息类型);
}
void LogInformation(string methodName,object information)=>Console.WriteLine($“\n方法名称:{methodName}\n信息:{information}”);
}
}
现在的问题是,当应用程序在后台时,我没有收到任何通知,也没有任何回调函数,如didReceivedNotif
public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
{
LoadApplication(new App());
Firebase.Core.App.Configure();
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
// iOS => 10
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) =>
{
//request authorization method
Console.WriteLine(granted);
});
// For iOS 10+ display notification (sent via APNS)
UNUserNotificationCenter.Current.Delegate = this;
}
else
{
// iOS 9 <=
var allNotificationTypes = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound;
var settings = UIUserNotificationSettings.GetSettingsForTypes(allNotificationTypes, null);
UIApplication.SharedApplication.RegisterUserNotificationSettings(settings);
}
UIApplication.SharedApplication.RegisterForRemoteNotifications();
return base.FinishedLaunching(application, launchOptions);
}