本地通知-鬧鐘

本地通知簡介:

本地通知簡單來說app本身給應(yīng)用程序推送消息,不需要服務(wù)器支持。使用場景:鬧鐘,定時操作。

1.創(chuàng)建本地通知

//? 創(chuàng)建本地通知對象

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

2.設(shè)置本地通知屬性

NSDateFormatter *date_formatter = [[NSDateFormatter alloc] init];

[date_formatter setDateFormat:@"yyyy-MM-dd"];

NSString *TimeStr = [date_formatter stringFromDate:[NSDate date]];

/** 目前是定時每天九點(diǎn),后續(xù)版本迭代在封裝 */

TimeStr = [NSString stringWithFormat:@"%@ 09:00:00",TimeStr];

[date_formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

NSDate *dateTime = [date_formatter dateFromString:TimeStr];

//1.設(shè)置推送類型


UIUserNotificationType type = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;

//2.設(shè)置setting

UIUserNotificationSettings *setting? = [UIUserNotificationSettings settingsForTypes:type categories:nil];

//3.主動授權(quán)

[[UIApplication sharedApplication] registerUserNotificationSettings:setting];

//時區(qū)

localNoti.timeZone = [NSTimeZone defaultTimeZone];

//時間

localNoti.fireDate = dateTime;

//重復(fù)間隔

localNoti.repeatInterval = kCFCalendarUnitDay;

//通知內(nèi)容

localNoti.alertBody = @"今日活動任務(wù)有到期";

//通知聲音

localNoti.soundName = UILocalNotificationDefaultSoundName;

//? ? //通知參數(shù)

//? ? localNoti.userInfo = [NSDictionary dictionaryWithObject:@"NotificationStr" forKey:@"Notification"];

//發(fā)送通知:

[[UIApplication sharedApplication] scheduleLocalNotification:localNoti];


3.取消通知

#pragma mark -- 取消通知

- (void)cancelLocalNotification

{

// 獲取所有本地通知數(shù)組

NSArray *localNotifications = [UIApplication sharedApplication].scheduledLocalNotifications;

for (UILocalNotification *notification in localNotifications) {

NSDictionary *userInfo = notification.userInfo;

if (userInfo) {

// 根據(jù)設(shè)置通知參數(shù)時指定的key來獲取通知參數(shù)

NSString *info = userInfo[@"Notification"];

// 如果找到需要取消的通知,則取消

if (info != nil) {

[[UIApplication sharedApplication] cancelLocalNotification:notification];

//圖標(biāo)的數(shù)字清零

([UIApplication sharedApplication].applicationIconBadgeNumber >= 1) ?([UIApplication sharedApplication].applicationIconBadgeNumber -= 1):0 ;

break;

}

}

}

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 一. 推送通知的介紹 什么是推送通知首先要明確一點(diǎn), 推送通知與通知Notification和通知中心等一點(diǎn)關(guān)系都...
    面糊閱讀 1,846評論 0 50
  • 概述 在多數(shù)移動應(yīng)用中任何時候都只能有一個應(yīng)用程序處于活躍狀態(tài),如果其他應(yīng)用此刻發(fā)生了一些用戶感興趣的那么通過通知...
    莫離_焱閱讀 6,717評論 1 8
  • //發(fā)送通知 UILocalNotification *notification=[[UILocalNotific...
    韓七夏閱讀 1,689評論 1 0
  • 第一步:創(chuàng)建本地推送 本地通知 UILocalNotification // 創(chuàng)建?一個本地推送 UILocalN...
    夜千尋墨閱讀 564評論 0 2
  • 01 樂清不是第一次來,這個浙江溫州的一個縣級市我還是很喜歡的。山清水秀的,南邊靠海,有海鮮海灘美不勝收,北部的雁...
    冀泰來閱讀 664評論 13 9

友情鏈接更多精彩內(nèi)容