ios - 如何从远程推送通知中获取 APNS 有效负载?
问题描述
我正在我的 OSX 应用程序中添加远程推送通知支持。我可以通过 apns 在我的 PC 中成功接收推送通知,当我单击通知横幅时,这就是我看到的行为。
如果我的应用程序没有运行,它将启动应用程序。但永远不会点击“ReceivedRemoteNotification()”。这是预期的行为吗?在这种情况下有什么方法可以接收 apns 有效载荷?
如果我的应用程序正在运行,我会通过“ReceivedRemoteNotification”获取有效负载,并且一切正常。
所以如果我们的应用程序没有运行,我们就无法在我们的应用程序中获取 apns 有效负载?
任何帮助都非常感谢。
谢谢,吉塞什
解决方案
弄清楚了。有效载荷可通过 DidFinishLaunching 的“NSNotification”参数获得。
public override void DidFinishLaunching(NSNotification notification)
{
var uInfo = notification.UserInfo;
if(uInfo["NSApplicationLaunchUserNotificationKey"] != null)
{
var payLoad = uInfo["NSApplicationLaunchUserNotificationKey"] as NSUserNotification;
if(payLoad.ActivationType == NSUserNotificationActivationType.ActionButtonClicked)
{
NSAlert alert = new NSAlert();
alert.MessageText = "Button Clicked";
alert.RunModal();
}
var userInfo = payLoad.UserInfo;
//not.ActivationType ==
if (userInfo != null && userInfo.ContainsKey(new NSString("aps")))
{
//Get the aps dictionary
NSDictionary aps = userInfo.ObjectForKey(new NSString("aps")) as NSDictionary;
if (aps.ContainsKey(new NSString("content")))
{
var deepLink = (aps[new NSString("content")] as NSString).ToString();
NSAlert alert = new NSAlert();
alert.MessageText = deepLink;
alert.RunModal();
//DisplaySubView(deepLink);
//Console.WriteLine($"Deeplink : {deepLink}");
}
}
}
推荐阅读
- sql - 如何链接两个用户,以便他们都可以查看相同的表 SQL*Plus
- android - 所有屏幕都应该可以滚动以考虑屏幕旋转吗?
- sql - 是否存在维恩图有意义的 SQL 连接的特殊情况?
- postgresql - 如何最好地索引这个表?
- dart - 如何通过换行迭代字符串内容
- r - 通过改变第一列中的值来拆分 data.frame
- wpf - 在一行语法中使用参数绑定转换器
- python - while 循环中的多个输入,以及 Python 中的 try-except 块
- prometheus - 有没有什么方法可以使用 prometheus 指标根据 CPU 内核来表示 POD CPU 使用率
- c# - 遍历 BehaviorSubject<> 并设置对象的 prop