#import "baseviewcontroller.h"
@interface notificagtionviewcontroller :
baseviewcontroller
// 設定本地通知
+ (void)registerlocalnotification:(nsinteger)alerttime;
+ (void)cancellocalnotificationwithkey:(nsstring *)key;
@property (weak,
nonatomic) iboutlet
uibutton *mybutton;
uitextfield *mytext;
@end
#import "notificagtionviewcontroller.h"
@interface
notificagtionviewcontroller ()<uitextfielddelegate>
@implementation notificagtionviewcontroller
- (void)viewdidload {
[super
viewdidload];
// do any additional setup after loading the view.
}
- (void)didreceivememorywarning {
didreceivememorywarning];
// dispose of any resources that can be recreated.
- (bool)textfieldshouldreturn:(uitextfield *)textfield{
[self.view
endediting: yes];
return
yes;
-(void)touchesbegan:(nsset<uitouch *> *)touches withevent:(uievent *)event{
+ (void)registerlocalnotification:(nsinteger)alerttime {
uilocalnotification *notification = [[uilocalnotification
alloc]
init];
// 設定觸發通知的時間
nsdate *firedate = [nsdate
datewithtimeintervalsincenow:alerttime];
nslog(@"firedate=%@",firedate);
notification.firedate = firedate;
//
時區
notification.timezone = [nstimezone
defaulttimezone];
設定重複的間隔
notification.repeatinterval =
kcfcalendarunitsecond;
通知内容
notification.alertbody =
@"adfdsafsagsdfsebsd";
notification.applicationiconbadgenumber =
1;
// 通知被觸發時播放的聲音
notification.soundname =
uilocalnotificationdefaultsoundname;
通知參數
nsdictionary *userdict = [nsdictionary
dictionarywithobject:@"開始學習ios開發了"
forkey:@"key"];
notification.userinfo = userdict;
// ios8後,需要添加這個注冊,才能得到授權
if ([[uiapplication
sharedapplication]
respondstoselector:@selector(registerusernotificationsettings:)]) {
uiusernotificationtype type =
uiusernotificationtypealert | uiusernotificationtypebadge |
uiusernotificationtypesound;
uiusernotificationsettings *settings = [uiusernotificationsettings
settingsfortypes:type
categories:nil];
[[uiapplication
sharedapplication] registerusernotificationsettings:settings];
// 通知重複提示的機關,可以是天、周、月
notification.repeatinterval =
nscalendarunitday;
} else {
nsdaycalendarunit;
}
執行通知注冊
[[uiapplication
sharedapplication] schedulelocalnotification:notification];
- (ibaction)gopush:(uibutton *)sender {
[notificagtionviewcontroller
registerlocalnotification:3];// 3秒後
// 取消某個本地推送通知
+ (void)cancellocalnotificationwithkey:(nsstring *)key {
// 擷取所有本地通知數組
nsarray *localnotifications = [uiapplication
sharedapplication].scheduledlocalnotifications;
for (uilocalnotification *notification
in localnotifications) {
nsdictionary *userinfo = notification.userinfo;
if (userinfo) {
// 根據設定通知參數時指定的key來擷取通知參數
nsstring *info = userinfo[key];
//
如果找到需要取消的通知,則取消
if (info !=
nil) {
[[uiapplication
sharedapplication] cancellocalnotification:notification];
break;
}
}
appdelegate ()
// 本地通知回調函數,當應用程式在前台時調用
- (void)application:(uiapplication *)application didreceivelocalnotification:(uilocalnotification *)notification {
nslog(@"noti:%@",notification);
// 這裡真實需要處理互動的地方
// 擷取通知所帶的資料
nsstring *notmess = [notification.userinfo
objectforkey:@"key"];
uialertview *alert = [[uialertview
initwithtitle:@"本地通知(前台)"
message:notmess
delegate:nil
cancelbuttontitle:@"ok"
otherbuttontitles:nil];
[alert show];
// 更新顯示的徽章個數
nsinteger badge = [uiapplication
sharedapplication].applicationiconbadgenumber;
badge--;
badge = badge >= 0 ? badge :
0;
[uiapplication
sharedapplication].applicationiconbadgenumber = badge;
// 在不需要再推送時,可以取消推送
cancellocalnotificationwithkey:@"key"];