我正在使用版本 8.2.1。问题是我在 IOS 10.3.1 中没有收到通知,但在 IOS 9(但两次)中收到了通知。
这是我尝试过的代码:
//In **didFinishLaunchingWithOptions**
#define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending)
if( SYSTEM_VERSION_LESS_THAN( @"10.0" ) )
{
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
// iOS 8 Notifications
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]];
[application registerForRemoteNotifications];
}
}
else
{
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
[center requestAuthorizationWithOptions:(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge) compl
etionHandler:^(BOOL granted, NSError * _Nullable error)
{
if( !error )
{
[[UIApplication sharedApplication] registerForRemoteNotifications]; // required to get the app to do anything at all about push notifications
NSLog( @"Push registration success." );
}
else
{
NSLog( @"Push registration FAILED" );
NSLog( @"ERROR: %@ - %@", error.localizedFailureReason, error.localizedDescription );
NSLog( @"SUGGESTIONS: %@ - %@", error.localizedRecoveryOptions, error.localizedRecoverySuggestion );
}
}];
}
if([self pushNotificationOnOrOff]) {
NSLog(@"Push Notification is ON!");
} else {
NSLog(@"Push Notification is OFF.");
}
if (launchOptions != nil)
{
NSDictionary* dictionary1 = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (dictionary1 != nil)
{
//RemoteNotification Payload
NSLog(@"Launched from push notification: %@", dictionary1);
Globals.instance.isNotification = TRUE;
NSString* iUUID = [[launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey] objectForKey:@"UUID"];
[self.window makeKeyAndVisible];
NSDictionary* dictData = [Database getDataForUUID:iUUID];
if (dictData){
//Show dictData
}
}
}
**// Push Notification Receiving Methods**
- (void)application:(UIApplication*)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData*)deviceToken {
NSString* deviceTokenStr = [NSString stringWithFormat:@"%@", deviceToken];
deviceTokenStr = [deviceTokenStr substringWithRange:NSMakeRange(1, deviceTokenStr.length-2)];
if (!Globals.instance.deviceToken) {
Globals.instance.deviceToken = deviceTokenStr;
}
}
- (void)application:(UIApplication*)application didFailToRegisterForRemoteNotificationsWithError:(NSError*)error {
NSLog(@"Failed to get token, error: %@", error);
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)notification {
NSDictionary* data = [self checkForData:notification];
if (data){
if (application.applicationState == UIApplicationStateActive) [self activeAlertForData: data];
else [self displayData: data];
}
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSLog(@"Userinfo %@",notification.request.content.userInfo);
completionHandler(UNNotificationPresentationOptionAlert);
}
-(void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler {
NSLog(@"Userinfo %@",response.notification.request.content.userInfo);
}
-(BOOL)pushNotificationOnOrOff
{
BOOL pushEnabled=NO;
if ([[UIApplication sharedApplication] isRegisteredForRemoteNotifications]) {
pushEnabled=YES;
} else {
pushEnabled=NO;
}
return pushEnabled;
}
- (NSDictionary*)checkForData:(NSDictionary*)notification {
NSString* iUUID = [notification objectForKey:@"UUID"];
NSDictionary* dict = [Database getUUID:iUUID];
NSString* alertData = [notification objectForKey:@"alert"];
return dict;
}
- (void)activeAlertForData:(Inc*)inc {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController * alert = [UIAlertController
alertControllerWithTitle:@"New Post”
message:inc.description
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* yesButton = [UIAlertAction
actionWithTitle:@"OK"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
}];
UIAlertAction* noButton = [UIAlertAction
actionWithTitle:@“Open”
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * action) {
// Jump to Post.
}];
[alert addAction:yesButton];
[alert addAction:noButton];
UIViewController *vc = [[[[UIApplication sharedApplication] delegate] window] rootViewController];
[vc presentViewController:alert animated:YES completion:nil];
});
}
请指导我解决此问题。
提前致谢。