zoukankan      html  css  js  c++  java
  • ios开发之-本地推送通知

    不多说直接上代码

    #import "AppDelegate.h"

    @interface AppDelegate ()

    @end

    @implementation AppDelegate

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

        // Override point for customization after application launch.

        //如果已经获得发送通知的授权则创建本地通知,否则请求授权(注意:如果不请求授权在设置中是没有对应的通知设置项的,也就是说如果从来没有发送过请求,即使通过设置也打不开消息允许设置)

        //if ([[UIApplication sharedApplication]currentUserNotificationSettings].types !=UIUserNotificationTypeNone) {

            [self addLocalNotification];

        //}else{

          //  [[UIApplication sharedApplication]registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound  categories:nil]];

        //}

        

        return YES;

    }

    #pragma mark 调用过用户注册通知方法之后执行(也就是调用完registerUserNotificationSettings:方法之后执行)

    -(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{

        if (notificationSettings.types!=UIUserNotificationTypeNone) {

            [self addLocalNotification];

        }

    }

    #pragma mark 进入前台后设置消息信息

    -(void)applicationWillEnterForeground:(UIApplication *)application{

        [[UIApplication sharedApplication]setApplicationIconBadgeNumber:0];//进入前台取消应用消息图标

    }

    #pragma mark - 私有方法

    #pragma mark 添加本地通知

    -(void)addLocalNotification{

        

        //定义本地通知对象

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

        //设置调用时间

        notification.fireDate=[NSDate dateWithTimeIntervalSinceNow:10.0];//通知触发的时间,10s以后

        notification.repeatInterval=2;//通知重复次数

        //notification.repeatCalendar=[NSCalendar currentCalendar];//当前日历,使用前最好设置时区等信息以便能够自动同步时间

        

        //设置通知属性

        notification.alertBody=@"最近添加了诸多有趣的特性,是否立即体验?"; //通知主体

        notification.applicationIconBadgeNumber=1;//应用程序图标右上角显示的消息数

        notification.alertAction=@"打开应用"; //待机界面的滑动动作提示

        notification.alertLaunchImage=@"Default";//通过点击通知打开应用时的启动图片,这里使用程序启动图片

        //notification.soundName=UILocalNotificationDefaultSoundName;//收到通知时播放的声音,默认消息声音

        notification.soundName=@"msg.caf";//通知声音(需要真机才能听到声音)

        

        //设置用户信息

        notification.userInfo=@{@"id":@1,@"user":@"Kenshin Cui"};//绑定到通知上的其他附加信息

        

        //调用通知

        [[UIApplication sharedApplication] scheduleLocalNotification:notification];

    }

    #pragma mark 移除本地通知,在不需要此通知时记得移除

    -(void)removeNotification{

        [[UIApplication sharedApplication] cancelAllLocalNotifications];

    }

  • 相关阅读:
    LALR(1)语法分析生成器--xbytes
    <<摩托车修理技术与禅>>读书笔记
    xscript脚本
    CentOS安装Erlang
    TCP中close和shutdown之间的区别
    Erlang高阶函数
    深度学习小记
    mac平台打造犀利的Android Studio开发环境
    MAC中如何配置两套android-sdk环境
    Ubuntu虚拟机编译Android6.0总结
  • 原文地址:https://www.cnblogs.com/godlovexq/p/5329169.html
Copyright © 2011-2022 走看看