美文网首页
本地推送的应用

本地推送的应用

作者: jianshu小赵 | 来源:发表于2016-08-08 20:14 被阅读62次

    今天我们来讲解一下 本地推送的小知识

    直接上代码  

    1,在appdelegate 里面注册本地通知 代码如下

    /**

    * 注册通知

    */

    - (void)registerLocalNotification  :(UIApplication *)application

    {

    // 注意: 在iOS8中, 必须提前注册通知类型

    if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {

    UIUserNotificationType type = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge | UIUserNotificationTypeSound;

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:type categories:nil];

    // 注册通知类型

    [application registerUserNotificationSettings:settings];

    }

    }

    接下来 可以在主页面 等页面 贴入以下代码  ,一定要监听到这个方法

    - (void)didReceiveMessages:(NSArray*)aMessages

    {

    EMMessage* message =

    aMessages[0];

    //ZLLog(@"%@-------%@",aMessages,message);

    NSString* from = message.from;

    EMMessageBody*body = message.body;

    if(body.type==EMMessageBodyTypeText) {

    //收到的文字消息

    EMTextMessageBody*textBody = (EMTextMessageBody*)body;

    NSString*txt = textBody.text;

    self.txt= txt;

    NSLog(@"收到的文字是txt -- %@",txt);

    }

    以上是我用  环信 监听消息回调的法子 可以忽略 下面是通知实现的代码。

    // 1.创建本地通知对象

    UILocalNotification*note = [[UILocalNotificationalloc]init];

    //指定通知发送的时间(指定5秒之后发送通知)

    note.fireDate= [NSDatedateWithTimeIntervalSinceNow:0];

    //注意:在真实开发中一般情况下还需要指定时区(让通知的时间跟随当前时区)

    note.timeZone= [NSTimeZonedefaultTimeZone];

    //指定通知内容

    note.alertBody= [NSStringstringWithFormat:@"%@ : %@",from,self.txt];

    //设置通知重复的周期(1分钟通知一期)

    //note.repeatInterval = NSCalendarUnitSecond;

    //指定锁屏界面的信息

    //note.alertAction = @"这是锁屏界面的信息";

    //设置点击通知进入程序时候的启动图片

    note.alertLaunchImage=@"LaunchImage";

    //设置震动

    AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//(#import

    //收到通知播放的音乐

    note.soundName=@"buyao.wav";

    //设置应用程序的提醒图标

    //note.applicationIconBadgeNumber = 998;

    //注册通知时可以指定将来点击通知之后需要传递的数据

    //note.userInfo =

    @{@"name":@"lnj",@"age":@"28",@"phone":

    @"12345678912"};

    // 2.注册通知(图片的名称建议使用应用程序启动的图片名称)

    UIApplication*app =[UIApplicationsharedApplication];

    //每次调用添加方法都会将通知添加到scheduledLocalNotifications数组中

    [appscheduleLocalNotification:note];

    }

    这样 我们就可以 实现本地通知了 接下来我们要在回调里

    ```

    相关文章

      网友评论

          本文标题:本地推送的应用

          本文链接:https://www.haomeiwen.com/subject/aoprsttx.html