远程推送

推送需要真机调试

 

#import "AppDelegate.h"

@interface AppDelegate ()

@end

 

@implementation AppDelegate

 

 

 

 

 

//注册完毕 远程推送通知之后就会调用

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

{

    NSLog(@"%@",deviceToken);

}

 

 

//远程通知注册失败的时候就会调用

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

{

    //常见失败的三种情况

    //1,BundliID没有用全称

    //2.描述文件没有在最后配置

    //3,AppleWWDRCA.cer 误删, 导致证书文件没有授权机构的签发

}

 

//接收到远程推送通知之后就会调用,前提,程序活着

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

{

    NSLog(@"%@",userInfo);

}

 

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

    // Override point for customization after application launch.

    //注册通知,自动提示用户通知包含的类型

    UIUserNotificationSettings *setting=[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeAlert | UIUserNotificationTypeSound categories:nil];

    [[UIApplication sharedApplication]registerUserNotificationSettings:setting];

    //注册远程推送通知 这种方法会打包 uuid appid 生成devicetoken

    [[UIApplication sharedApplication]registerForRemoteNotifications];

    

    

    //取出通知内容

    if(launchOptions[UIApplicationLaunchOptionsLocalNotificationKey])

    {

        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 320, 300)];

        label.numberOfLines = 0;

        label.backgroundColor = [UIColor redColor];

        label.text= [NSString stringWithFormat:@"%@",launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]];

        [self.window.rootViewController.view addSubview:label];

    }

    

    

    return YES;

}

原文地址:https://www.cnblogs.com/tangranyang/p/4655852.html