如果应用程序在后台使用方法didReceiveRemoteNotification
,我设法处理推送通知。如果应用程序位于前台而未关闭,是否有办法处理推送通知?感谢
答案 0 :(得分:1)
您是否已实施UserNotification?如果您在iOS 10+上部署项目,可以尝试在FinishedLaunching(UIApplication application, NSDictionary launchOptions)
中订阅通知,如:
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
var authOptions = UNAuthorizationOptions.Alert | UNAuthorizationOptions.Badge | UNAuthorizationOptions.Sound;
UNUserNotificationCenter.Current.RequestAuthorization(authOptions, (granted, error) => {
Console.WriteLine(granted);
});
UNUserNotificationCenter.Current.Delegate = new MyNotificationCenterDelegate();
}
然后,当通知到来并且应用程序在前台时,WillPresentNotification()
将会触发,此句柄事件将返回此通知到来时此应用响应的操作。最后,您可以在DidReceiveNotificationResponse()
事件中获取userInfo:
public class MyNotificationCenterDelegate : UNUserNotificationCenterDelegate
{
public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
completionHandler();
}
public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
{
completionHandler(UNNotificationPresentationOptions.Sound | UNNotificationPresentationOptions.Alert);
}
}
但是如果你不想实现UserNotification
或者你只是按下content-available
键的无声远程通知,那么didReceiveRemoteNotification()
也会被触发,即使该应用是在前景。
答案 1 :(得分:1)
要处理推送通知事件,无论应用程序在前台还是后台运行。按照以下步骤操作,可以始终调用DidReceiveRemoteNotification
1)覆盖AppDelegate.cs中的DidReceiveRemoteNotification方法
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
2)将必需的后台模式-远程通知(值)添加到info.plist
3)将有效负载中的可用内容设置为1
{
"aps" : {
"alert" : {
"title" : "Game Request",
"body" : "Bob wants to play poker"
},
"badge" : 5,
"content-available" : 1
}
}
答案 2 :(得分:0)
当您在前台时收到推送通知时,ReceivedRemoteNotification
中的覆盖方法DidReceiveRemoteNotification
或AppDelegate
会被触发。
在此方法中,您可以检查您的应用是否处于Active
状态,并根据它,您可以处理您的通知。
if (UIApplication.SharedApplication.ApplicationState == UIApplicationState.Active)
{
//You app is in Foreground state.
//Process the Pushnotification data, or show alert.
}
实施 应用(:didReceiveRemoteNotification:fetchCompletionHandler :) 尽可能的方法而不是这个方法。如果你的代表 实现这两种方法,app对象调用 应用(:didReceiveRemoteNotification:fetchCompletionHandler :) 方法
因此,如果您在AppDelegate中实施了两种方法(ReceivedRemoteNotification
&amp; DidReceiveRemoteNotification
),则会调用DidReceiveRemoteNotification
方法。
答案 3 :(得分:0)
我在Xamarin iOS中通过以下示例app.delegte.cs代码获得了前台和后台通知。
using Foundation;
using System;
using System.Diagnostics;
using System.Linq;
using UIKit;
using UserNotifications;
using WindowsAzure.Messaging;
using Firebase.Core;
using Firebase.CloudMessaging;
using System.Runtime.CompilerServices;
using ObjCRuntime;
namespace NotificationHubSample.iOS
{ [Register(“ AppDelegate”)]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
private SBNotificationHub Hub { get; set; }
public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());
base.FinishedLaunching(app, options);
Firebase.Core.App.Configure();
RegisterForRemoteNotifications();
UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);
return true;
}
void RegisterForRemoteNotifications()
{
// register for remote notifications based on system version
if (UIDevice.CurrentDevice.CheckSystemVersion(10, 0))
{
UNUserNotificationCenter.Current.RequestAuthorization(UNAuthorizationOptions.Alert |
UNAuthorizationOptions.Sound |
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
{
UIRemoteNotificationType notificationTypes = UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound;
UIApplication.SharedApplication.RegisterForRemoteNotificationTypes(notificationTypes);
}
UIApplication.SharedApplication.RegisterForRemoteNotifications();
}
public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
Hub = new SBNotificationHub(AppConstants.ListenConnectionString, AppConstants.NotificationHubName);
// update registration with Azure Notification Hub
Hub.UnregisterAll(deviceToken, (error) =>
{
if (error != null)
{
Debug.WriteLine($"Unable to call unregister {error}");
return;
}
var tags = new NSSet(AppConstants.SubscriptionTags.ToArray());
Hub.RegisterNative(deviceToken, tags, (errorCallback) =>
{
if (errorCallback != null)
{
Debug.WriteLine($"RegisterNativeAsync error: {errorCallback}");
}
});
var templateExpiration = DateTime.Now.AddDays(120).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
Hub.RegisterTemplate(deviceToken, "defaultTemplate", AppConstants.APNTemplateBody, templateExpiration, tags, (errorCallback) =>
{
if (errorCallback != null)
{
if (errorCallback != null)
{
Debug.WriteLine($"RegisterTemplateAsync error: {errorCallback}");
}
}
});
});
// Firebase ios Registration
Messaging.SharedInstance.ApnsToken = deviceToken;
}
// Process the notification when the app is open.
public override void ReceivedRemoteNotification(UIApplication application, NSDictionary userInfo)
{
ProcessNotification(userInfo, false);
} // End of the ReceiveRemoteNotification method
// Process the notification when the app is closed.
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
ProcessNotification(userInfo, false);
}
void ProcessNotification(NSDictionary options, bool fromFinishedLaunching)
{
// make sure we have a payload
if (options != null && options.ContainsKey(new NSString("aps")))
{
// get the APS dictionary and extract message payload. Message JSON will be converted
// into a NSDictionary so more complex payloads may require more processing
NSDictionary aps = options.ObjectForKey(new NSString("aps")) as NSDictionary;
string payload = string.Empty;
NSString payloadKey = new NSString("alert");
if (aps.ContainsKey(payloadKey))
{
payload = aps[payloadKey].ToString();
}
if (!string.IsNullOrWhiteSpace(payload))
{
(App.Current.MainPage as MainPage)?.AddMessage(payload);
}
}
else
{
Debug.WriteLine($"Received request to process notification but there was no payload.");
}
} // End of the ProcessNotification method
}
}