zoukankan      html  css  js  c++  java
  • 本地通知 LocalNotification

    本地通知 LocalNotification 

    在Appdelegate中实现以下两个方法

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        //设置桌面红点内的数字
    //    application.applicationIconBadgeNumber = 0;
        
        //请求通知权限
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeAlert | UIUserNotificationTypeSound | UIUserNotificationTypeBadge) categories:nil];
        [application registerUserNotificationSettings:settings];
        
        return YES;
    }

    收到本地通知的代理方法

    1.点击本地通知弹框

    2.程序在前台, 收到本地通知

    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification {
        NSLog(@"%@", notification);
    }

    添加一个button到ViewController中

    #import "ViewController.h"
    
    @interface ViewController ()
    - (IBAction)localNoti:(id)sender;
    @end
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad]; 
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    
    - (IBAction)localNoti:(id)sender {
        //创建本地通知
        UILocalNotification *notification = [[UILocalNotification alloc] init];
        //提示框内容
        notification.alertBody = @"起床啦";
        //提示框标题
        notification.alertTitle = @"醒啦";
        //通知声音
        notification.soundName = UILocalNotificationDefaultSoundName;
        //触发时间
        notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
        //icon旁边的数字
        notification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1;
        //本地通知重复
        notification.repeatInterval = NSCalendarUnitMinute;
        //发送通知
        [[UIApplication sharedApplication] scheduleLocalNotification:notification];
    }
    @end

    cmd + shift + H 退出程序, 本地通知就弹出

     

     

  • 相关阅读:
    nginx 优化
    linux 内核的优化
    Linux下如何查看版本
    oracle安装数据库中文乱码解决办法
    Python 5 行代码的神奇操作
    Python爬取网站上面的数据很简单,但是如何爬取APP上面的数据呢
    解放双手!用 Python 控制你的鼠标和键盘
    js混淆、eval解密
    ubuntu
    爬虫基本原理
  • 原文地址:https://www.cnblogs.com/OrangesChen/p/5071403.html
Copyright © 2011-2022 走看看