Qt for iOS:推送一个本地通知到通知中心
2015-01-06 21:18
477 查看
本文讲述如何进行一个本地通知的推送
这个推送相对简单,如果各位有兴趣可以去研究一下网络通知的推送,那个更加的强大和灵活
那么,本地的通知,可以做哪些呢?
比如说你***了一个游戏,你可以埋一个一天期限的通知,然后用户在第二天就可以看到
“你已经一天没玩XX了,赶快来玩~”
其实,虽然只是本地的,但是这个通知已经可以满足很多需求了
关于本地通知的几个知识点:
1.在添加了通知后,通知就加入到了系统的推送列表(如果这是一个有效的通知,并且用户允许你的app进行推送),在你清除之前,他会一直在那里直到触发
2.在添加了通知后,无论程序当前状态是关闭或者打开亦或是在后台,通知都可以正常推送
3.当前app就是你的app时,推送不会直接显示出来,只会默默的到通知中心
4.这个通知,只可以在程序运行期生成
不多说,直接上源码
注意,本项目需要C++与Objective-C混编,也就是说,这个做法,仅用于iOS
JasonQt_iOS.h文件
JasonQt_iOS.mm文件(注意是mm后缀,可以理解为cpp。只有使用mm后缀的时候,才会开启C++和Objective-C混编模式)
使用
注:使用前,请在pro文件中添加
效果1,在通知中心查看:
![](http://img.blog.csdn.net/20150106212232390?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3NqMTg4MDgwNTA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
效果2,在其他地方查看:
![](http://img.blog.csdn.net/20150106212311624?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3NqMTg4MDgwNTA=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
示例下载地址
http://download.csdn.net/detail/wsj18808050/8714083
这个推送相对简单,如果各位有兴趣可以去研究一下网络通知的推送,那个更加的强大和灵活
那么,本地的通知,可以做哪些呢?
比如说你***了一个游戏,你可以埋一个一天期限的通知,然后用户在第二天就可以看到
“你已经一天没玩XX了,赶快来玩~”
其实,虽然只是本地的,但是这个通知已经可以满足很多需求了
关于本地通知的几个知识点:
1.在添加了通知后,通知就加入到了系统的推送列表(如果这是一个有效的通知,并且用户允许你的app进行推送),在你清除之前,他会一直在那里直到触发
2.在添加了通知后,无论程序当前状态是关闭或者打开亦或是在后台,通知都可以正常推送
3.当前app就是你的app时,推送不会直接显示出来,只会默默的到通知中心
4.这个通知,只可以在程序运行期生成
不多说,直接上源码
注意,本项目需要C++与Objective-C混编,也就是说,这个做法,仅用于iOS
JasonQt_iOS.h文件
#ifndef __JasonQt_iOS_h__ #define __JasonQt_iOS_h__ // Qt lib import #include <QString> #include <QDateTime> namespace JasonQt_iOS { void pushNotifiction(const QString &message, const QDateTime &dateTime); void cancelAllNotifictions(void); } #endif//__JasonQt_iOS_h__
JasonQt_iOS.mm文件(注意是mm后缀,可以理解为cpp。只有使用mm后缀的时候,才会开启C++和Objective-C混编模式)
#include "JasonQt_iOS.h" // iOS lib import #import <Foundation/Foundation.h> #import <UIKit/UIKit.h> void JasonQt_iOS::pushNotifiction(const QString &message, const QDateTime &dateTime) { UIUserNotificationType types = UIUserNotificationTypeAlert | UIUserNotificationTypeBadge; UIUserNotificationSettings *setting = [UIUserNotificationSettings settingsForTypes:types categories:NULL]; [[UIApplication sharedApplication] registerUserNotificationSettings:setting]; NSDateComponents *dateComp = [[NSDateComponents alloc] init]; dateComp.year = dateTime.date().year(); dateComp.month = dateTime.date().month(); dateComp.day = dateTime.date().day(); dateComp.hour = dateTime.time().hour(); dateComp.minute = dateTime.time().minute(); dateComp.second = dateTime.time().second(); dateComp.timeZone = [NSTimeZone systemTimeZone]; NSCalendar *calendar = [NSCalendar calendarWithIdentifier:NSGregorianCalendar]; NSDate *date = [calendar dateFromComponents:dateComp]; UILocalNotification *notifiction = [[UILocalNotification alloc]init]; notifiction.alertBody = [NSString stringWithFormat:@"%s" , message.toStdString().c_str()]; notifiction.fireDate = date; [[UIApplication sharedApplication]scheduleLocalNotification:notifiction]; } void JasonQt_iOS::cancelAllNotifictions(void) { [[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; [[UIApplication sharedApplication] cancelAllLocalNotifications]; }
使用
JasonQt_iOS::pushNotifiction("Message: Be overdue", QDateTime::currentDateTime().addSecs(-60)); // 过期的信息不会被显示 JasonQt_iOS::pushNotifiction("Message: Need to cancel", QDateTime::currentDateTime().addSecs(15)); // 被取消的信息不会被显示 JasonQt_iOS::cancelAllNotifictions(); for(int count = 0; count < 3; count++) { JasonQt_iOS::pushNotifiction(QString("Message: %1").arg(count + 1), QDateTime::currentDateTime().addSecs(16 + count * 2)); }
注:使用前,请在pro文件中添加
LIBS += -framework Foundation -framework UIKit OBJECTIVE_SOURCES += JasonQt_iOS.mm HEADERS += JasonQt_iOS.h
效果1,在通知中心查看:
效果2,在其他地方查看:
示例下载地址
http://download.csdn.net/detail/wsj18808050/8714083
相关文章推荐
- [IOS 推送消息] 将旧的推送通知从通知中心移除掉
- iOS 本地推送通知
- iOS 设置每天下午4点推送本地通知
- iOS 消息推送及本地通知,原理解析
- iOS 注册本地通知(推送)
- iOS本地推送与取消本地通知—UILocalNotification的使用
- iOS关于本地推送通知的简单用法
- iOS推送:本地通知UILocalNotification
- iOS 设置每天下午4点推送本地通知
- (七十三)iOS本地推送通知的实现
- ios推送:本地通知UILocalNotification
- iOS推送:本地通知UILocalNotification
- IOS_本地通知_消息推送_微博_蓝牙_ImagePicker
- Qt for iOS:使用UIImagePickerController获取一个系统相册中的图片
- ios推送:本地通知UILocalNotification
- IOS本地推送通知
- iOS本地通知和远程推送
- ios推送:本地通知UILocalNotification
- [转]->ios推送:本地通知UILocalNotification
- iOS之推送通知-本地-服务器3.服务器端实现: 如果要编写内容提供者的推送服务程序,需要进行SSL认证编程,以及构建APNS数据包,数据包分为3个主要部分:Command(命令)、deviceTo