zoukankan      html  css  js  c++  java
  • 使用UILocalNotification给App添加本地消息通知

    使用过的代码,直接贴上

     1 UILocalNotification *notification = [[UILocalNotification alloc] init];
     2   if (notification!=nil) {
     3     NSDate *now = [NSDate new];
     4     //从现在开始,10秒以后通知
     5     notification.fireDate=[now addTimeInterval:10];
     6     //使用本地时区
     7     notification.timeZone=[NSTimeZone defaultTimeZone];
     8     notification.alertBody=@"顶部提示内容,通知时间到啦";
     9     //通知提示音 使用默认的
    10     notification.soundName= UILocalNotificationDefaultSoundName;
    11     notification.alertAction=NSLocalizedString(@"你锁屏啦,通知时间到啦", nil);
    12     //这个通知到时间时,你的应用程序右上角显示的数字。
    13     notification.applicationIconBadgeNumber = 1;
    14     NSDictionary *dic = [NSDictionary dictionaryWithObject:@"name" forKey:@"key"];
    15     notification.userInfo = dic;
    16     //启动这个通知
    17     [[UIApplication sharedApplication]scheduleLocalNotification:notification];
    18 }

    需要注意的是在iOS8之后需要注册消息推送服务才可以,具体实现就在AppDelegate的

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

    方法中直接调用下面方法即可

    1 if ([UIApplication instancesRespondToSelector:@selector(registerUserNotificationSettings:)]) {
    2       [application registerUserNotificationSettings:[UIUserNotificationSettings 
    3                                    settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound 
    4                                          categories:nil]];
    5 }
  • 相关阅读:
    Python 字符串和list的功能翻译
    python .strip()
    python 查看对象功能
    python 字典
    洛谷 P1144 最短路计数 Label:水
    心疼自己,再见
    初赛复习 //附复习资料
    51Nod 1079 中国剩余定理 Label:数论
    转载 乘法逆元
    51Nod 1136 欧拉函数 Label:数论
  • 原文地址:https://www.cnblogs.com/jackma86/p/4982169.html
Copyright © 2011-2022 走看看