iOS 本地推送 UILocalNotification

來源:互聯網
上載者:User

iOS 本地推送 UILocalNotification
建立通知

 

 

UILocalNotification *notification = [[UILocalNotification alloc] init];    if (notification)    {        NSDate *now = [NSDate new];        notification.fireDate = [now dateByAddingTimeInterval:10]; //10秒後通知        notification.repeatInterval=0; //重複次數,kCFCalendarUnitWeekday一周一次        notification.timeZone = [NSTimeZone defaultTimeZone]; //設定時區        notification.applicationIconBadgeNumber = 1; //應用的角標        notification.soundName = UILocalNotificationDefaultSoundName; //聲音,可以換成alarm.soundName = @sound.wav        //去掉下面2行就不會彈出提示框        notification.alertBody = @通知內容; //提示資訊 彈出提示框        notification.alertAction = @開啟; //提示框按鈕        //notification.hasAction = NO; //是否顯示額外的按鈕,為no時alertAction消失        NSDictionary *infoDict = [NSDictionary dictionaryWithObject:@value forKey:@key];        //設定userinfo 方便在之後需要撤銷的時候使用 也可以傳遞其他值,當通知觸發時可以擷取        notification.userInfo = infoDict;                [[UIApplication sharedApplication] scheduleLocalNotification:notification];    }

 


推送的內容
//推送的內容- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification*)notification{    //這裡,你就可以通過notification的useinfo,幹一些你想做的事情了    if ([[notification.userInfo objectForKey:@key] isEqualToString:@value])    {           UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@系統提示 message:@你的系統存在嚴重威脅 delegate:nil cancelButtonTitle:@關閉 otherButtonTitles:nil,nil];           [alert show];            }    application.applicationIconBadgeNumber = 0; //移除角標}

- (void)applicationDidBecomeActive:(UIApplication *)application{    //不通過推送 通過應用表徵圖開啟應用 移除角標    application.applicationIconBadgeNumber = 0;}

取消通知
//擷取當前所有的本地通知    NSArray *notificaitons = [[UIApplication sharedApplication] scheduledLocalNotifications];    if (!notificaitons || notificaitons.count <= 0)    {        return;    }    //取消一個特定的通知    for (UILocalNotification *notify in notificaitons)    {        if ([[notify.userInfo objectForKey:@key] isEqualToString:@value])        {            [[UIApplication sharedApplication] cancelLocalNotification:notify];            break;        }    }    //    //取消所有的本地通知//    [[UIApplication sharedApplication] cancelAllLocalNotifications];



 

相關文章

聯繫我們

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