本地推送

APPDelegate.h


- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

{

// 如果應(yīng)用程序在前臺(tái),將應(yīng)用程序圖標(biāo)上紅色徽標(biāo)中數(shù)字設(shè)為0

application.applicationIconBadgeNumber = 0;

// 使用UIAlertView顯示本地通知的信息

[[[UIAlertView alloc] initWithTitle:@"收到通知"

message:notification.alertBody

delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil] show];

}

ViewController.M ? 拖拽按鈕


@interface ViewController (){

UIApplication *app;

}

@end


- (void)viewDidLoad {

[super viewDidLoad];

app = [UIApplication sharedApplication];

}


UISwitch *swith = (UISwitch *)sender;

if (swith.on) {

if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {

[[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];

}

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

//設(shè)置一個(gè)通知觸發(fā)時(shí)間

notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:5];

//設(shè)置一個(gè)通知時(shí)區(qū)

notification.timeZone = [NSTimeZone defaultTimeZone];

// 設(shè)置通知的重復(fù)發(fā)送的事件間隔

notification.repeatInterval = kCFCalendarUnitHour;

// 設(shè)置通知的聲音

notification.soundName = @"gu.mp3";

//通知標(biāo)題

notification.alertTitle=@"洪恩英語(yǔ)就是牛逼";

// 設(shè)置當(dāng)設(shè)備處于鎖屏狀態(tài)時(shí),顯示通知的警告框下方的title

notification.alertAction = @"打開(kāi)";

// 設(shè)置通知是否可顯示Action

notification.hasAction = YES;

// 設(shè)置通過(guò)通知加載應(yīng)用時(shí)顯示的圖片

notification.alertLaunchImage = @"logo.png";

// 設(shè)置通知內(nèi)容

notification.alertBody = @"洪恩英語(yǔ)NB !";

// 設(shè)置顯示在應(yīng)用程序上紅色徽標(biāo)中的數(shù)字

notification.applicationIconBadgeNumber = 1;

NSDictionary *info = @{@"bys":@"key"};

notification.userInfo = info;

[app scheduleLocalNotification:notification];

}else{

NSArray *arr = [app scheduledLocalNotifications];

if (arr) {

for (UILocalNotification * noti in arr)

{

NSDictionary *dic = noti.userInfo;

if (dic) {

NSString *str = [dic objectForKey:@"key"];

if([str isEqualToString:@"bys"])

{

[app cancelLocalNotification:noti];

}

}

}

}

}

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

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

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