iOS 本地通知

來源:互聯網
上載者:User

iOS 本地通知


#import "AppDelegate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//如果已經獲得發送通知的授權則建立本地通知,否則請求授權(注意:如果不請求授權在設定中是沒有對應的通知設定項的,也就是說如果從來沒有發送過請求,即使通過設定也打不開訊息允許設定)
#if 0
if ([[UIApplication sharedApplication]currentUserNotificationSettings].types!=UIUserNotificationTypeNone) {
[self addLocalNotification];
}else{
[[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
#else
//添加通知
[self addLocalNotification];
//接收通知參數
UILocalNotification *notification=[launchOptions valueForKey:UIApplicationLaunchOptionsLocalNotificationKey];
NSDictionary *userInfo= notification.userInfo;

[userInfo writeToFile:@"/Users/kenshincui/Desktop/didFinishLaunchingWithOptions.txt" atomically:YES];
NSLog(@"didFinishLaunchingWithOptions:The userInfo is %@.",userInfo);
#endif
return YES;
}
#pragma mark - 私人方法
#pragma mark 添加本地通知
- (void)addLocalNotification{
//定義本地通知對象
UILocalNotification *notification=[[UILocalNotification alloc]init];
//設定調用時間
notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10.0];//通知觸發的時間,10s以後
notification.repeatInterval=2;//通知重複次數
notification.repeatCalendar=[NSCalendar currentCalendar];//當前日曆,使用前最好設定時區等資訊以便能夠自動同步時間

//設定通知屬性
notification.alertBody=@"最近添加了諸多有趣的特性,是否立即體驗?"; //通知主體
notification.applicationIconBadgeNumber=1;//應用程式圖示右上方顯示的訊息數
notification.alertAction=@"開啟應用"; //待機介面的滑動動作提示
notification.alertLaunchImage=@"Default";//通過點擊通知開啟應用時的啟動圖片,這裡使用程式啟動圖片
//notification.soundName=UILocalNotificationDefaultSoundName;//收到通知時播放的聲音,預設訊息聲音
notification.soundName=@"msg.caf";//通知聲音(需要真機才能聽到聲音)

//設定使用者資訊
notification.userInfo=@{@"id":@1,@"user":@"Kenshin Cui"};//綁定到通知上的其他附加資訊

//調用通知
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
#pragma mark 調用過使用者註冊通知方法之後執行(也就是調用完registerUserNotificationSettings:方法之後執行)
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
if (notificationSettings.types!=UIUserNotificationTypeNone) {
[self addLocalNotification];
}
}
- (void)applicationWillResignActive:(UIApplication *)application {
}

- (void)applicationDidEnterBackground:(UIApplication *)application {

}
#pragma mark 進入前台後設定訊息資訊
- (void)applicationWillEnterForeground:(UIApplication *)application {
[[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];//進入前台取消應用訊息表徵圖
}
#pragma mark 移除本地通知,在不需要此通知時記得移除
-(void)removeNotification{
[[UIApplication sharedApplication] cancelAllLocalNotifications];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {

}

- (void)applicationWillTerminate:(UIApplication *)application {

}
#pragma mark 接收本地通知時觸發 222222
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
NSDictionary *userInfo=notification.userInfo;
[userInfo writeToFile:@"/Users/kenshincui/Desktop/didReceiveLocalNotification.txt" atomically:YES];
NSLog(@"didReceiveLocalNotification:The userInfo is %@",userInfo);
}

@end
注意:

在使用通知之前必須註冊通知類型,如果使用者不允許應用程式發送通知,則以後就無法發送通知,除非使用者手動到iOS設定中開啟通知。
本地通知是有作業系統統一調度的,只有在應用退出到後台或者關閉才能收到通知。(注意:這一點對於後面的推播通知也是完全適用的。 )
通知的聲音是由iOS系統播放的,格式必須是Linear PCM、MA4(IMA/ADPCM)、μLaw、aLaw中的一種,並且播放時間必須在30s內,否則將被系統聲音替換,同時自訂音效檔必須放到main boundle中。
本地通知的數量是有限制的,最近的本地通知最多隻能有64個,超過這個數量將被系統忽略。
如果想要移除本地通知可以調用UIApplication的cancelLocalNotification:或cancelAllLocalNotifications移除指定通知或所有通知。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.