天天看点

iOS 本地通知的实现  iOS 本地通知的实现

 iOS 本地通知的实现

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154

<pre code_snippet_id=

"1707319"

snippet_file_name=

"blog_20160604_1_563489"

name=

"code"

class

=

"objc"

>/<span style=

"font-size:18px;"

>/

#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (

BOOL

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

// 如果已经获得发送通知的授权则创建本地通知, 否则请求授权, 注意: 如果不请求授权在设置中没有对应的通知设置, 如果从来没有发送过请求, 即使通过设置也打不开消息允许设置

if

([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {

[self addLocationNotification];

}

else

{

// 必须要注册通知类型

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil]];

}

// Override point for customization after application launch.

return

YES;

}

#pragma mark ---- 调用过用户注册通知方法之后执行(调用完 registerUserNotificationSettings:之后执行的)

-(

void

)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings

{

if

(notificationSettings.types != UIUserNotificationTypeNone) {

[self addLocationNotification];

}

}

#pragma mark ---- 进入前台后设置消息信息

- (

void

)applicationWillEnterForeground:(UIApplication *)application

{

// 进入前台取消应用消息图标

[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];

}

#pragma mark ---- 添加本地通知

- (

void

)addLocationNotification

{

// 定义本地通知对象

UILocalNotification *notification  = [[ UILocalNotification alloc] init];

// 设置调用时间----- 当前时间10秒之后

notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10.0];

// 通知重复次数

notification.repeatInterval = 2;

// 当前日历, 使用前对号设置时区等信息, 使其同步

//    notification.repeatCalendar = [NSCalendar currentCalendar];

//多长时间重复:如果设置为NSCalendarUnitSecond,那么消息不会重复,每秒发送一次通知,iOS系统当然不会容许这样的存在了。

    location.repeatInterval =NSCalendarUnitSecond;

// 设置通知的属性

// 1. 通知的主体 body

notification.alertBody = @

"有好玩的东西, 快来尝试吧"

;

// 2. 应用程序图标左上角显示的消息数字

notification.applicationIconBadgeNumber = 1;

// 3. 待机界面的滑动动作提示

notification.alertAction = @

"打开应用"

;

// 4. 通过点击通知打开应用时的启动图片, 这里使用默认图片(注意必须有启动图片再行,在iOS9.0以后这个属性不太灵了)

notification.alertLaunchImage = @

"Default"

;

// 5. 收到通知时播放的声音, 默认消息声音

notification.soundName = UILocalNotificationDefaultSoundName; 

// 默认系统通知声音

   // 通知声音, 自定义

//    location.soundName = UILocalNotificationDefaultSoundName;

//    NSString* soundPath = [[NSBundle mainBundle] pathForResource:@"key-01" ofType:@"caf"];

//    location.soundName = soundPath;

// 6. 设置用户信息

notification.userInfo = @{@

"ID"

: @1, @

"user"

: @

"xiayan"

}; 

// 绑定通知上的其他附加信息

// 7. 调用通知

[[UIApplication sharedApplication] scheduleLocalNotification:notification];

}

#pragma mark ---- 移除本地通知, 在不需要的此通知时移除通知

- (

void

)removeNotification

{

// 设置取消通知

[[UIApplication sharedApplication] cancelAllLocalNotifications];

}

- (

void

)applicationWillResignActive:(UIApplication *)application {

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (

void

)applicationDidEnterBackground:(UIApplication *)application {

// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (

void

)applicationDidBecomeActive:(UIApplication *)application {

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (

void

)applicationWillTerminate:(UIApplication *)application {

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end</span></pre>