APNs 远程推送

APNs 远程推送

 

生成推送证书

    1.登陆开发者中心: https://developer.apple.com

    2.点开 certificates identifiers proversionprofiles 里面

    3.创建APPID. 勾选 push notification servers服务

    4.搜索刚才创建的APPID, 点击edit, 配置证书, 下载双击

    5.证书创建完成, 钥匙链内右键导出p12

    1.App key

    2.真机测试

注: 推送消息的实现只能在真机中实现

添加框架和极光推送的APService.h及SDK(可以再极光推送中下载JPush Demo)

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [APService registerForRemoteNotificationTypes:(UIUserNotificationTypeBadge | UIUserNotificationTypeSound|
        UIUserNotificationTypeAlert) categories:nil];
    [APService setupWithOption:launchOptions];
    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
    NSLog(@"%@", deviceToken);
    [APService registerDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo {
    // Required
    [APService handleRemoteNotification:userInfo];
}

- (void)application:(UIApplication *)application
didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void
                        (^)(UIBackgroundFetchResult))completionHandler {
    [APService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
    NSLog(@"收到通知:%@", userInfo);
  
    ViewController *VC = [[ViewController alloc] init];
        VC.string = userInfo[@"aps"][@"alert"];
    NSLog(@"S: %@", VC.string);
    [[NSNotificationCenter defaultCenter] postNotificationName:VC.string object:nil userInfo:@"str"];
}

- (void)application:(UIApplication *)application
didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
    NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
}

- (NSString *)logDic:(NSDictionary *)dic {
    if (![dic count]) {
        return nil;
    }
    NSString *tempStr1 =
    [[dic description] stringByReplacingOccurrencesOfString:@"\u"
                                                 withString:@"\U"];
    NSString *tempStr2 =
    [tempStr1 stringByReplacingOccurrencesOfString:@""" withString:@"\""];
    NSString *tempStr3 =
    [[@""" stringByAppendingString:tempStr2] stringByAppendingString:@"""];
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];
    NSString *str =
    [NSPropertyListSerialization propertyListFromData:tempData
                                     mutabilityOption:NSPropertyListImmutable
                                               format:NULL
                                     errorDescription:NULL];
    NSLog(@"str %@", str);

    return str;
}

 

原文地址:https://www.cnblogs.com/OrangesChen/p/5071501.html