PUSH相关

2013/01/21 16:30
阅读数 135
- (void)application:(UIApplication *)application
      didReceiveRemoteNotification:(NSDictionary *)userInfo 
didReceiveRemoteNotification 是在forground的时候调用

做了很多有Push Notification和Locale Notification的app,来整理下开发的笔记。今天先说说Local Notification

在iOS4.0后Apple加入了Local Notification。这里是NSLocaleNotification的Class Reference http://developer.apple.com/library/ios/#documentation/iPhone/Reference/UILocalNotification_Class/Reference/Reference.html


遍历已经注册的所有LocaleNotification

NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];  
    for (UILocalNotification *notification in notifications ) {  
        if( [[notification.userInfo objectForKey:@"source"] isEqualToString:@"dailyReminder"] ) {  
            [[UIApplication sharedApplication] cancelLocalNotification:notification];  
            break;  
        }  
    }

[[UIApplication sharedApplication] cancelAllLocalNotifications]; // 撤销所有的Notification
[UIApplication&nbsp;sharedApplication]&nbsp;cancelLocalNotification:notification];&nbsp;</span><span class="comment">//&nbsp;撤销某个Notificiation,若要删除某个特定的Notification,则可以在UserInfo中加入标记,遍历所有的Notification来删除。</span><span>&nbsp;&nbsp;</span></span></li></ol></div><pre name="code" class="cpp" style="display: none;">[[UIApplication sharedApplication] cancelLocalNotification:notification]; // 撤销某个Notificiation,若要删除某个特定的Notification,则可以在UserInfo中加入标记,遍历所有的Notification来删除。

响应事件
当Notification被触发后,你的应用需要对此作出反应。应用此时可能会处于以下几个状态:
>在前台运行 - 当应用在前台运行时,则ApplicationDelegate的didReceiveLocalNotification会被调用。- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
    NSLog(@"Notification Body: %@", notification.alertBody);
    NSLog(@"%@", notification.userInfo);
    application.applicationIconBadgeNumber = notification.applicationIconBadgeNumber-1;
}
在后台运行 - 用户可以看到类似Push Notification的提醒,若用户选择查看提醒详情,则应用通过 ApplicationDelegate的didFinishLaunchingWithOptions进入- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    UILocalNotification *localNotification =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    if (localNotification) {
        NSLog(@"Notification Body: %@",localNotification.alertBody);
        NSLog(@"%@", localNotification.userInfo);
        application.applicationIconBadgeNumber = localNotification.applicationIconBadgeNumber-1;
    }
    // set up everything else
    return YES;



push center 不消失的解决:

http://stackoverflow.com/questions/7773584/can-i-programmatically-clear-my-apps-notifications-from-the-ios-5-notification/7821985#7821985

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部