gpt4 book ai didi

iOS UIBackgroundMode 远程通知不适用于 4G

转载 作者:技术小花猫 更新时间:2023-10-29 11:24:27 29 4
gpt4 key购买 nike

我正在使用 content-available=1 测试推送通知,除非在 Wi-Fi 上,否则它们似乎不会在后台传送到应用程序。

我在推送通知处理程序的开头有一个简单的日志语句:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^) (UIBackgroundFetchResult))completionHandler {

NSLog(@"Notification received: %@", userInfo);
completionHandler(UIBackgroundFetchResultNewData);
}

这是我的测试:

  1. 运行应用,然后按主页按钮将应用置于后台。
  2. 发送 content-available=1 的推送通知
  3. 查看控制台日志

在 Wi-Fi 上,控制台日志显示通知。如果我转到“设置”并关闭 Wi-Fi,切换到 4G,通知将不再出现在日志中(尽管它们确实滑入屏幕顶部,所以我知道它们正在传送)。

没有崩溃日志,如果我手动点击它会记录通知。此外,如果我在 Xcode 中调试应用程序,则不会出现此问题。 (即,如果我在 Xcode 中调试,应用程序将在 4G 的后台接收通知)。有没有其他人经历过这种行为?还是我做错了什么?

编辑:具体来说:根据我的测试,如果满足以下条件,则不会调用上面的远程通知委托(delegate)方法:

  1. 应用程序在后台运行
  2. 手机使用 LTE 网络,未连接 Wi-Fi
  3. 应用程序未在 Xcode 调试器中运行
  4. 手机收到 content-available=1 的通知

但是,如果条件 2 被移除(即手机已连接到 Wi-Fi),则将调用处理程序。

最佳答案

试试下面的代码:

// AppDelegate.h

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{

NSString *DeviceToken;
NSMutableDictionary *App_Messages;

NSString *Longitude,*Latitude;
NSMutableDictionary * badge;
}

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewcontrollervc;

@property (strong, nonatomic) UINavigationController *navcontroller;

@property (nonatomic,retain)NSMutableDictionary *badge;

@property (nonatomic,retain)NSString *DeviceToken;

// AppDelegate.m

#import "ViewController.h"

@implementation AppDelegate

@synthesize badge,DeviceToken;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

self.window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
self.viewcontrollervc = [[ViewController alloc]initWithNibName:@"ViewController" bundle:nil];
self.navcontroller = [[UINavigationController alloc]initWithRootViewController:self.viewcontrollervc];
self.window.rootViewController = self.navcontroller;
self.navcontroller.navigationBarHidden = YES;


//Notification
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:
(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
NSDictionary * remoteNotificationObj = [launchOptions objectForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
if (remoteNotificationObj)
{
[self performSelector:@selector(handleRemoteNotificationWithUserInfo:) withObject:remoteNotificationObj afterDelay:3.0];
}

[self.window makeKeyAndVisible];
return YES;

}


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

[self handleRemoteNotificationWithUserInfo:userInfo];

}

-(void)handleRemoteNotificationWithUserInfo:(NSDictionary *)userInfo
{

NSLog(@"userInfo - %@",userInfo);
NSDictionary *alertData = [userInfo objectForKey:@"aps"];
NSDictionary *returnDatalert=[alertData objectForKey:@"alert"];
NSString *alertmsg=[returnDatalert objectForKey:@"body"];

NSLog(@"alertmsg %@",alertmsg);
self.badge = [NSMutableDictionary dictionaryWithDictionary:[alertData objectForKey:@"badge"]];
NSString *notificationtype=[badge objectForKey:@"fnct"];

NSLog(@"%@",notificationtype);

}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{

NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken: %@", deviceToken);
NSString *dt = [[deviceToken description] stringByTrimmingCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"<>"]];
dt = [dt stringByReplacingOccurrencesOfString:@" " withString:@""];
self.DeviceToken=dt;
NSLog(@"~~~~devToken(dv)=%@",deviceToken);

}

- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error
{

NSLog(@"Failed to get token, error: %@", error);

}

关于iOS UIBackgroundMode 远程通知不适用于 4G,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24928590/

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