通知

本地通知

按鈕方法 中 添加如下 代碼

//創建本地通知對象
UILocalNotification *localNotification = [[UILocalNotification alloc] init];

//設定調度時間,即通知五秒后執行
NSDate *nowDate = [NSDate date];
[localNotification setFireDate:[nowDate dateByAddingTimeInterval:5]];

//設定時區
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];

//設置彈出消息
[localNotification setAlertBody:@"搶購時間到了"];
[localNotification setAlertAction:@"show now"];

//設置通知聲音
[localNotification setSoundName:UILocalNotificationDefaultSoundName];

//設置IconbadgeNumber
[localNotification setApplicationIconBadgeNumber:1];

//應用程序計劃執行通知
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

IOS9之后 推出的 通知框

//通知框 ios 9
UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"標題" message:@"UIAlertController" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"通知框");
    }];
[alertController addAction:okAction];
    
[self presentViewController:alertController animated:YES completion:nil];

AppDelegate 中添加

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //判斷當前設備的版本
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge |UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    
    return YES;
}
//接收到的本地通知
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {

//    [self.window.rootViewController presentViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#> completion:<#^(void)completion#>];
    
    //將badge標記數減1
    [UIApplication sharedApplication].applicationIconBadgeNumber--;
    
//    self.window.rootViewController = [];

}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容