
1.如果隻是純粹的通知下面的代碼就可以實作了
- (void)viewDidLoad {
[superviewDidLoad];
[self SendLocalNotification];
}
-(void)SendLocalNotification{
UILocalNotification *localNotification = [[UILocalNotificationalloc]init];
//觸發通知時間
localNotification.fireDate = [NSDatedateWithTimeIntervalSinceNow:0.1];
//重複間隔
// localNotification.repeatInterval = kCFCalendarUnitMinute;
localNotification.timeZone = [NSTimeZonedefaultTimeZone];
//通知内容
localNotification.alertBody =@"家裡老人摔倒了";
localNotification.applicationIconBadgeNumber =1;
localNotification.soundName =UILocalNotificationDefaultSoundName;
//通知參數
localNotification.userInfo =@{@"kLocalNotificationKey":@"去吧,皮卡丘"};
localNotification.category =@"kNotificationCategoryIdentifile";
[[UIApplicationsharedApplication]scheduleLocalNotification:localNotification];
}
2.如果還想增加贊和評論的功能繼續添加下面代碼
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[selfregisterLocalNotification];
returnYES;
}
- (void)registerLocalNotification
{
//建立消息上面要添加的動作
UIMutableUserNotificationAction *action1 = [[UIMutableUserNotificationActionalloc]init];
action1.identifier =kNotificationActionIdentifileStar;
action1.title =@"贊";
//當點選的時候不啟動程式,在背景處理
action1.activationMode =UIUserNotificationActivationModeBackground;
//需要解鎖才能處理(意思就是如果在鎖屏界面收到通知,并且使用者設定了螢幕鎖,使用者點選了贊不會直接進入我們的回調進行處理,而是需要使用者輸入螢幕鎖密碼之後才進入我們的回調),如果action.activationMode = UIUserNotificationActivationModeForeground;則這個屬性被忽略;
action1.authenticationRequired =YES;
action1.destructive =NO;
//第二個動作
UIMutableUserNotificationAction *action2 = [[UIMutableUserNotificationActionalloc]init];
action2.identifier =kNotificationActionIdentifileComment;
action2.title =@"評論";
//當點選的時候不啟動程式,在背景處理
action2.activationMode =UIUserNotificationActivationModeBackground;
//設定了behavior屬性為 UIUserNotificationActionBehaviorTextInput的話,則使用者點選了該按鈕會出現輸入框供使用者輸入
action2.behavior =UIUserNotificationActionBehaviorTextInput;
//這個字典定義了當使用者點選了評論按鈕後,輸入框右側的按鈕名稱,如果不設定該字典,則右側按鈕名稱預設為 “發送”
action2.parameters =@{UIUserNotificationTextInputActionButtonTitleKey:@"評論"};
//建立動作(按鈕)的類别集合
UIMutableUserNotificationCategory *category = [[UIMutableUserNotificationCategoryalloc]init];
//這組動作的唯一标示
category.identifier =kNotificationCategoryIdentifile;
//最多支援兩個,如果添加更多的話,後面的将被忽略
[categorysetActions:@[action1, action2]forContext:(UIUserNotificationActionContextMinimal)];
//建立UIUserNotificationSettings,并設定消息的顯示類類型
UIUserNotificationSettings *uns = [UIUserNotificationSettingssettingsForTypes:(UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound)categories:[NSSetsetWithObject:category]];
[[UIApplicationsharedApplication]registerUserNotificationSettings:uns];
}
//本地通知回調函數,當應用程式在前台時調用
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(@"%@", notification.userInfo);
[selfshowAlertView:@"使用者沒點選按鈕直接點的推送消息進來的/或者該app在前台狀态時收到推送消息"];
NSInteger badge = [UIApplicationsharedApplication].applicationIconBadgeNumber;
badge -= notification.applicationIconBadgeNumber;
badge = badge >=0 ? badge :0;
[UIApplicationsharedApplication].applicationIconBadgeNumber = badge;
}
//在非本App界面時收到本地消息,下拉消息會有快捷回複的按鈕,點選按鈕後調用的方法,根據identifier來判斷點選的哪個按鈕,notification為消息内容
- (void)application:(UIApplication *)application handleActionWithIdentifier:(nullableNSString *)identifier forLocalNotification:(UILocalNotification *)notification withResponseInfo:(NSDictionary *)responseInfo completionHandler:(void(^)())completionHandler
{
if ([identifierisEqualToString:kNotificationActionIdentifileStar]) {
[selfshowAlertView:@"點了贊"];
}elseif ([identifierisEqualToString:kNotificationActionIdentifileComment]) {
[selfshowAlertView:[NSStringstringWithFormat:@"使用者評論為:%@", responseInfo[UIUserNotificationActionResponseTypedTextKey]]];
}
completionHandler();
}
- (void)showAlertView:(NSString *)message
{
UIAlertController *alert = [UIAlertControlleralertControllerWithTitle:nilmessage:messagepreferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *action = [UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:nil];
[alertaddAction:action];
[self.window.rootViewControllershowDetailViewController:alertsender:nil];
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
[UIApplicationsharedApplication].applicationIconBadgeNumber =0;
}