Qt for ios本地推送(LocalPush)
作者:melon
日期:2019/7/22
最终效果图:
1.项目配置
(1)环境配置:
项目配置macOs10.13 + Qt5.10.0 + xcode9.4(注意Qt版本和xcode版本兼容,具体信息可到官网查看),以下是5.10支持的平台配置
配置不对可能会报错Undefined symbols for architecture x86_64: "_backtrace_from_fp", referenced from:
QMacAutoReleasePool::QMacAutoReleasePool()in libQt5Core_debug.a(qcore_mac_objc.o)
ld: symbol(s) not found for architecture x86_64
(2)Qt项目配置:
.mm这种格式的文件可以同时兼容object-c和c/c++格式的代码,实现多种语言的混编,但是需要在.pro文件OBJECTIVE_SOURCES添加进去,用到ios的框架也需要添加进去,格式为 LIBS += -framework [框架名称]
不然会报错:Undefined symbols for architecture x86_64
2.本地推送流程
(1)注册通知,获取用户授权
void JasonQt_iOS::registerAPN()
{
// 注册通知
qDebug() << NSFoundationVersionNumber;
if (NSFoundationVersionNumber >= NSFoundationVersionNumber_iOS_9_0) { // iOS10 以上
NSLog(@"Register apn 10.0");
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
[center requestAuthorizationWithOptions:(UNAuthorizationOptionAlert + UNAuthorizationOptionSound)
completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
} else {// iOS8.0 以上
NSLog(@"Register apn 8.0");
UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert categories:nil];
[[UIApplication sharedApplication] registerUserNotificationSettings:setting];
}
NSLog(@"Register end");
}
Qt有时候的语法高亮可以忽略,编译能够通过,运行也没问题,以是否能够正常编译为准
(2)添加通知
void JasonQt_iOS::pushNotifiction()
{
if (@available(iOS 10.0, *)){
NSLog(@"push notification");
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
// 标题
content.title = @"测试标题";
content.subtitle = @"测试通知副标题";
// 内容
content.body = @"测试通知的具体内容";
// 声音
// content.sound = [UNNotificationSound defaultSound];
content.sound = [UNNotificationSound soundNamed:@"Alert_ActivityGoalAttained_Salient_Haptic.caf"];
// 角标 (我这里测试的角标无效,暂时没找到原因)
//content.badge = @1;
// 多少秒后发送,可以将固定的日期转化为时间
NSTimeInterval time = [[NSDate dateWithTimeIntervalSinceNow:10] timeIntervalSinceNow];
// NSTimeInterval time = 10;
// repeats,是否重复,如果重复的话时间必须大于60s,要不会报错
UNTimeIntervalNotificationTrigger *trigger = [UNTimeIntervalNotificationTrigger triggerWithTimeInterval:time repeats:NO];
/*
//如果想重复可以使用这个,按日期
// 周一早上 8:00 上班
NSDateComponents *components = [[NSDateComponents alloc] init];
// 注意,weekday默认是从周日开始
components.weekday = 2;
components.hour = 8;
UNCalendarNotificationTrigger *calendarTrigger = [UNCalendarNotificationTrigger triggerWithDateMatchingComponents:components repeats:YES];
*/
// 添加通知的标识符,可以用于移除,更新等操作
NSString *identifier = @"noticeId";
UNNotificationRequest *request = [UNNotificationRequest requestWithIdentifier:identifier content:content trigger:trigger];
[center addNotificationRequest:request withCompletionHandler:^(NSError *_Nullable error) {
NSLog(@"成功添加推送");
}];
}else {
UILocalNotification *notif = [[UILocalNotification alloc] init];
// 发出推送的日期
notif.fireDate = [NSDate dateWithTimeIntervalSinceNow:10];
// 推送的内容
notif.alertBody = @"你已经10秒没出现了";
// 可以添加特定信息
//notif.userInfo = @{@"noticeId":@"00001"};
// 角标
notif.applicationIconBadgeNumber = 1;
// 提示音
notif.soundName = UILocalNotificationDefaultSoundName;
// 每周循环提醒
notif.repeatInterval = NSCalendarUnitWeekOfYear;
[[UIApplication sharedApplication] scheduleLocalNotification:notif];
}
}
通过UNMutableNotificationContent设置通知的内容包括标题、声音、角标等,通过UNTimeIntervalNotificationTrigger设置通知的时间,最后把这些设置结合起来生成UNNotificationRequest最后通过UNUserNotificationCenter去处理这个请求就ok了,本例没有去实现检查应用通知授权以及移除通知这些功能,如若要实现,可自行百度。
备注:
(1)注册通知类型,当用户不允许时,则app无法接收通知;(用户可以到iOS设置->通知->找到相应app,手动设置通知选项)
(2)远程通知与本地通知是由iOS统一调度的,只有在应用退出到后台或者关闭才能收到通知。
(3)通知的声音是在代码中指定,由系统播放,播放时间必须在30s内,否则将被系统声音替换,同时自定义声音文件必须放到main boundle中
(4)本地通知是有数量限制的,一般短时间内发送的本地通知上限为64个,超过这个数量将被系统忽略(数据来源于网络,具体时间间隔待验证)
源码下载链接:https://download.csdn.net/download/weixin_28927079/11424830
参考链接:https://blog.csdn.net/weixin_33698043/article/details/87978918