gpt4 book ai didi

c# - 应用程序被杀死时无法接收 APNS

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:33 24 4
gpt4 key购买 nike

我正在使用 xamarin 开发 iOS 应用程序。现在我遇到了这个问题,我想从服务器向连接的设备发送推送通知。当我的应用程序运行时以及当我的应用程序处于后台时,我可以收到这些通知。现在的问题是,当我的应用程序被杀死时,我能收到通知吗? (主页按钮向上滑动)?我读到了它,虽然不可靠,但应该是可能的。但是,我该怎么做呢?

我阅读了有关通过启动选项获取通知的信息

public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)

但它始终为 Null 并且无法调试它,因为我正在关闭我的应用程序。关于我做错了什么的想法?

附言我正在使用开发配置文件(也许这就是问题所在?)

编辑 1:额外信息(代码)

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
application.RegisterForRemoteNotificationTypes (UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound);

if (launchOptions != null)
{
// check for a local notification
if (launchOptions.ContainsKey(UIApplication.LaunchOptionsRemoteNotificationKey))
{
var localNotification = launchOptions[UIApplication.LaunchOptionsRemoteNotificationKey];
if (localNotification != null)
{

new UIAlertView("TESTER", "TESTER3", null, "OK", null).Show();
// reset our badge
UIApplication.SharedApplication.ApplicationIconBadgeNumber = 0;
}
}
}
return true;
}

public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
{
Console.WriteLine (deviceToken.ToString ());
String tokenStr = deviceToken.Description;
var deviceToken1 = tokenStr.Replace ("<", "").Replace (">", "").Replace (" ", "");
ViewController.deviceToken = deviceToken1;
}

public override void DidReceiveRemoteNotification (UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
Console.WriteLine ("Recieved");
var message = userInfo.ValueForKey (new NSString ("aps")).ValueForKey (new NSString ("message")).ToString ();
var reload = userInfo.ValueForKey (new NSString ("aps")).ValueForKey (new NSString ("refreshList")).ToString ();
var vehicleId = userInfo.ValueForKey (new NSString ("aps")).ValueForKey (new NSString ("vehicleListId")).ToString ();
}

==> 使用 3 个变量(带有信息)做一些事情

最佳答案

首先,可以确保您所有的证书都是正确的(例如:沙箱服务器使用开发证书,生产服务器使用用户生产证书)

其次,您需要用两种不同的方法处理推送通知。我不确定 C# 或 Xamarin 中的代码,Objective-C 中的代码如下:-

//Receive Push Notification when the app is active in foreground or background
- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {

if(userInfo){
//TODO: Handle the userInfo here
}
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Get the push notification when app is not open
NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];

if(remoteNotif){
[self handleRemoteNotification:application userInfo:remoteNotif];
}

return YES;
}

-(void)handleRemoteNotification:(UIApplication*)application userInfo:(NSDictionary*)userInfo{

if(userInfo){
//TODO: Handle the userInfo here
}
}

关于c# - 应用程序被杀死时无法接收 APNS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25618454/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com