iOS 註冊本地通知(推送)

來源:互聯網
上載者:User

標籤:ios   本地通知   本地推送   uilocalnotification   

        註:按Home鍵讓App進入後台運行時,方可查看通知。


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

   // Override point for customization after application launch.

    

   // 註冊本地通知

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

    if (localnotifit) {

        

      // 擷取通知時間

        NSDate *now = [NSDate date];

        localnotifit.timeZone = [NSTimeZone defaultTimeZone];

        

       // 10秒後開始通知

        localnotifit.fireDate  = [now dateByAddingTimeInterval:10.0];

        

       // 重複間隔 (下限為每分鐘通知一次)

        localnotifit.repeatInterval = kCFCalendarUnitMinute;

        

       // 提醒內容

        localnotifit.alertBody = @"十秒後手機將會爆炸,趕快扔掉";

        

        //鎖屏狀態下,“滑動來”(這三個字是系統自動出現的)後面緊接著文字就是alertAction

        localnotifit.alertAction = @"解鎖(進入App)";

        

       // 通知欄裡的通知標題

        localnotifit.alertTitle = @"提示";

        

        //預設的通知聲音(只有在真機上才會聽到)

        localnotifit.soundName = UILocalNotificationDefaultSoundName;

        

       // 紅色圈圈數字

        localnotifit.applicationIconBadgeNumber = 1;

        

       // 通知標識

        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObject:@"LocalNotificationID" forKey:@"key"];

        localnotifit.userInfo = dic;

        

        //將通知添加到系統中

        [[UIApplication sharedApplication] scheduleLocalNotification:localnotifit];

        

    }

    

   // 註冊通知(iOS 8之後的註冊通知方法,iOS 8以前暫不做介紹)

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound categories:nil];

    [[UIApplication sharedApplication] registerUserNotificationSettings:settings];

    

    return YES;

}


// 接收到通知後觸發的方法,只有在App進入前台的時候才會執行。

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

{

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"恭喜你上當了" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"我很開心", nil];

    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

    [alert show];

    

    //取消通知

//    [[UIApplication sharedApplication] cancelAllLocalNotifications];

}



iOS 註冊本地通知(推送)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.