注册通知推送

//进行本地推送
-(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];
            }
        }
        
    });//全局
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容