iOS 10本地推送

来源:互联网 发布:java语言的四个特点 编辑:程序博客网 时间:2024/06/07 08:33
////  AppDelegate.m//  localNotification////  Created by lcy on 17/4/17.//  Copyright © 2017年 ZG. All rights reserved.//#import "AppDelegate.h"#import <UserNotifications/UserNotifications.h>@interface AppDelegate ()<UNUserNotificationCenterDelegate>@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];    center.delegate = self;    //一:请求获取通知权限(角标,声音,弹框)    [center requestAuthorizationWithOptions:(UNAuthorizationOptionBadge | UNAuthorizationOptionSound | UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {        if (granted) {            //获取用户是否同意开启通知            NSLog(@"request authorization successed!");        }else{            NSLog(@"不允许通知,到设置画面更改!!!");        }    }];    [self addNotification];    return YES;}-(void)addNotification{    //第二步:新建通知内容对象    UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];    content.title = @"iOS10通知";    content.subtitle = @"新通知学习笔记";    content.body = @"新通知变化很大,之前本地通知和远程推送是两个类,现在合成一个了。这是一条测试通知,";    content.badge = @1;    UNNotificationSound *sound = [UNNotificationSound defaultSound];    content.sound = sound;    // 图片    NSString *imageFile = [[NSBundle mainBundle] pathForResource:@"sport" ofType:@"png"];    UNNotificationAttachment *imageAttachment = [UNNotificationAttachment attachmentWithIdentifier:@"iamgeAttachment" URL:[NSURL fileURLWithPath:imageFile] options:nil error:nil];    content.attachments = @[imageAttachment];    //第三步:通知触发机制。(重复提醒,时间间隔要大于60s)    UNTimeIntervalNotificationTrigger *trigger1 = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:10 repeats:NO];    //第四步:创建UNNotificationRequest通知请求对象    NSString *requertIdentifier = @"RequestIdentifier";    UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requertIdentifier content:content trigger:trigger1];    //第五步:将通知加到通知中心    [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {        NSLog(@"Error:%@",error);    }];}- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler{    NSLog(@"前台收到通知");    completionHandler(UNNotificationPresentationOptionAlert);}- (void)applicationWillEnterForeground:(UIApplication *)application{    application.applicationIconBadgeNumber = 0;}

这里写图片描述

0 0
原创粉丝点击