iOS远程通知

来源:互联网 发布:mysql 余数 编辑:程序博客网 时间:2024/06/11 12:54

远程通知原理就不在解释了,直接上代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    //iOS10之前    //通知设置    UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge|UIUserNotificationTypeSound|UIUserNotificationTypeAlert categories:nil];    [application registerUserNotificationSettings:setting];    //注册远程通知获取deviceToken    [application registerForRemoteNotifications];    //判断是否通过点击通知打开APP的    if(launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]){        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{            //将未读消息数设置为0            application.applicationIconBadgeNumber = 0;            //显示通知内容            UITextView *textView = [[UITextView alloc] initWithFrame:[UIScreen mainScreen].bounds];            textView.backgroundColor = [UIColor yellowColor];            textView.textColor = [UIColor redColor];            textView.text = [NSString stringWithFormat:@"%@",launchOptions];            [application.keyWindow addSubview:textView];        });    }    return YES;}- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {    //注册远程通知成功    NSLog(@"%@",deviceToken.description);}- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {    //注册远程通知失败    NSLog(@"fail");}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {    //打开app时接收到通知    NSLog(@"%@",userInfo);}

iOS推送小工具
编译后会报错,做如下修改就好了

#include <CoreServices/../Frameworks/CarbonCore.framework/Headers/MacTypes.h>替换为#include <MacTypes.h>

将通知程序拖进工程里,在ApplicationDelegateinit()方法里修改对应环境证书的名字

原创粉丝点击