IOS本地通知

來源:互聯網
上載者:User

標籤:

   

發送通知:  

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

    if (newNotification) {

//時區

        newNotification.timeZone=[NSTimeZone defaultTimeZone];

//推送事件---10秒後

        newNotification.fireDate=[[NSDate date] dateByAddingTimeInterval:10];

        //推送內容

        newNotification.alertBody = @"訊號警示";

//應用右上方紅色表徵圖數字

        newNotification.applicationIconBadgeNumber = 1;

註:

//1:格式一定要支援播放,常用的格式caf

//2:音頻播放時間不能大於30秒

//3:在Resource裡要找到音頻檔案,倒入時最好能點項目名稱右鍵add匯入

        newNotification.soundName = @"jingBao2.caf";

//設定按鈕

newNotification.alertAction = @"關閉";

        //判斷重複與否

        newNotification.repeatInterval = NSWeekCalendarUnit;

//存入的字典,用於傳入資料,區分多個通知 

        NSMutableDictionary *dicUserInfo = [[NSMutableDictionary alloc] init];

        [dicUserInfo setValue:@"" forKey:@"clockID"];

        float floatHeng = userLocation.location.coordinate.latitude;

        float floatShu = userLocation.location.coordinate.longitude;

        [dicUserInfo setValue:[NSString stringWithFormat:@"%f",strX] forKey:@"heng"];

        [dicUserInfo setValue:[NSString stringWithFormat:@"%f",strY] forKey:@"shu"];

        newNotification.userInfo = [NSDictionary dictionaryWithObject:dicUserInfo forKey:@"dictionary"];

        [dicUserInfo release];

        [[UIApplication sharedApplication] scheduleLocalNotification:newNotification];

    }

    NSLog(@"Post new localNotification:%@", newNotification);

    [newNotification release];

    [pool release];

 

取消通知:

通知完一定要取消,IOS最多允許最近本地通知數量是64個,超過限制的本地通知將被忽略。

1:刪除應用所有通知

[[UIApplication sharedApplication] cancelAllLocalNotifications];

2:根據字典刪除個別通知 

key:發送通知時候傳入的字典值來判斷是哪個推送

 

    for (int i=0; i<[myArray count]; i++) {

        UILocalNotification    *myUILocalNotification=[myArray objectAtIndex:i];

        if ([[[myUILocalNotification userInfo] objectForKey:@"key"] intValue][email protected]"字典值") {

            [[UIApplication sharedApplication] cancelLocalNotification:myUILocalNotification];

        }

    }

 

通知執行完調用的方法 AppDelegate.m類裡面

//推送完 執行的事件

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

//notification是發送通知時傳入的字典資訊

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"標題" message:notification.alertBody delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];

        [alert show];

[alert release];

}

最後還有一個地方:執行通知一定要退出應用才能收到通知。

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.