通知

AppDelegate.m

-------------------

@implementation AppDelegate

//只有當(dāng)應(yīng)用在前臺(tái)時(shí),該方法才會(huì)被調(diào)用

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

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

application.applicationIconBadgeNumber = 0;

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

[[[UIAlertView alloc]initWithTitle:@"收到通知" message:notification.alertBody delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil] show];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

// Override point for customization after application launch.

return YES;

}

-----------------------------------

#import "ViewController.h"

@interface ViewController ()

{

UIApplication *app;

}

@end

@implementation ViewController

- (void)viewDidLoad {

[super viewDidLoad];

app = [UIApplication sharedApplication];

}

- (IBAction)changed:(id)sender{

UISwitch *sw = (UISwitch *)sender;

if (sw.on) {

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

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

}

//創(chuàng)建一個(gè)本地通知

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

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

notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:20];

//設(shè)置通知的時(shí)區(qū)

notification.timeZone = [NSTimeZone defaultTimeZone];

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

notification.repeatInterval = kCFCalendarUnitMinute;

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

notification.soundName = @"";

//通知標(biāo)題

notification.alertTitle=@"啦啦啦";

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

notification.alertAction = @"OPEN";

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

notification.hasAction = YES;

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

notification.alertLaunchImage = @"";

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

notification.alertBody = @"主人,妞妞想你了!";

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

notification.applicationIconBadgeNumber = 1;

// 設(shè)置userinfo,用于攜帶額外的附加信息。

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

notification.userInfo = info;

// 調(diào)度通知

[app scheduleLocalNotification:notification];? // ①

}

else

{

// 獲取所有處于調(diào)度中本地通知數(shù)組

NSArray *localArray = [app scheduledLocalNotifications];

if (localArray)

{

for (UILocalNotification *noti in localArray)

{

NSDictionary *dict = noti.userInfo;

if (dict)

{

// 如果找到要取消的通知

NSString *inKey = [dict objectForKey:@"bys"];

if ([inKey isEqualToString:@"key"])

{

// 取消調(diào)度該通知

[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)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

  • 概述 在多數(shù)移動(dòng)應(yīng)用中任何時(shí)候都只能有一個(gè)應(yīng)用程序處于活躍狀態(tài),如果其他應(yīng)用此刻發(fā)生了一些用戶(hù)感興趣的那么通過(guò)通知...
    莫離_焱閱讀 6,717評(píng)論 1 8
  • //發(fā)送通知 UILocalNotification *notification=[[UILocalNotific...
    韓七夏閱讀 1,691評(píng)論 1 0
  • 許多集成的步驟個(gè)推官網(wǎng)都有了,這里只寫(xiě)關(guān)于推送的遠(yuǎn)程推送和本地通知的步驟和代碼。APP在后臺(tái)時(shí):走蘋(píng)果的APNS通...
    AllureJM閱讀 2,963評(píng)論 1 9
  • 陸家村 十一 十一 那人也看到了孫云,然后立馬站起來(lái),拉住了孫云的手,問(wèn)了孫云最近怎么樣,然后讓孫云坐下...
    昭平湖閱讀 177評(píng)論 0 1
  • 最近在學(xué)習(xí)英文,不,準(zhǔn)確的說(shuō)是重拾記憶,依舊犯著老毛病,就是不敢張嘴說(shuō),晚上聽(tīng)喜馬拉雅口語(yǔ)音頻的時(shí)...
    Miss伊柚閱讀 300評(píng)論 1 4

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