美文网首页
注册通知推送

注册通知推送

作者: univer2012 | 来源:发表于2017-08-11 09:29 被阅读10次
    //进行本地推送
    -(void)receiveLocalNotificaionWithDictionary:(NSDictionary *)dict {
        dispatch_barrier_async(dispatch_get_global_queue(0, 0), ^{
            CGFloat deviceFloat = [[[UIDevice currentDevice] systemVersion] floatValue];
            if (deviceFloat >= 10.0) {
    #ifdef __IPHONE_10_0
                NSLog(@"LocalNotification");
                //Local Notification
                UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
                content.body = dict[@"description"];//内容
                content.sound = [UNNotificationSound defaultSound];
                content.userInfo = dict;
                content.categoryIdentifier = dict[@"msgId"];
                //content.badge = @1;
                //10秒后提醒
                UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:3 repeats:NO];
                NSString *requestIdentifier = dict[@"msgId"];//@"sampleRequest";
                UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:requestIdentifier content:content trigger:trigger];
                
                [[UNUserNotificationCenter currentNotificationCenter] addNotificationRequest:request withCompletionHandler:^(NSError * _Nullable error) {
                    if (error == nil) {
                        NSLog(@"Time Interval Notification scheduled: %@",requestIdentifier);
                    }
                    else {
                        NSLog(@"error : %@",error);
                    }
                }];
    #endif
            }
            else {
                UILocalNotification* notification=[[UILocalNotification alloc] init];
                if (nil != notification) {
                    UIApplication *application = [UIApplication sharedApplication];
                    //            application.applicationIconBadgeNumber ++;
                    notification.fireDate = [NSDate date];
                    notification.timeZone= [NSTimeZone defaultTimeZone];
                    notification.alertBody = [NSString stringWithFormat:@"%@",dict[@"description"]];
                    //如果app不在前台打开提示音
                    if(application.applicationState != UIApplicationStateActive){
                        //通知提示音 使用默认的
                        notification.soundName = UILocalNotificationDefaultSoundName;
                    }else{
                        notification.soundName = @"";
                    }
                    notification.userInfo = dict;
                    //            AudioServicesPlaySystemSound(1004); //更换播放系统的声音 id值可以百度查询
                    //            AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);//震动
                    // 启动通知
                    NSLog(@"scheduleLocalNotification");
                    [[UIApplication sharedApplication]scheduleLocalNotification:notification];
                }
            }
            
        });//全局
    }
    

    相关文章

      网友评论

          本文标题:注册通知推送

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