使用 quickblox 和 IOS sdk 实现预定的推送通知

Posted

技术标签:

【中文标题】使用 quickblox 和 IOS sdk 实现预定的推送通知【英文标题】:Implementing scheduled push notifications with quickblox and IOS sdk 【发布时间】:2014-04-28 09:39:34 【问题描述】:

在我的应用程序中,我想发送“预定”推送通知。我正在使用 quickblox 作为推送通知服务。推送通知必须从应用程序本身创建,以便由 quickblox 在设置的日期/时间发送。消息的目的是发送内容可用标志,以便应用可以在后台获取数据。

搜索文档,我认为这可以通过“ApplePushEvent”来实现。但是,我找不到有关此的任何进一步信息。我尝试在 sendButtonDidPress 部分添加以下代码,以向自己发送延迟 5 分钟的推送通知:

NSUInteger userID = ((QBUUser *)[_users objectAtIndex:[usersPickerView selectedRowInComponent:0]]).ID;


NSDate *date = [[NSDate alloc]init];
NSDate *fiveMinFromNow = [date dateByAddingTimeInterval:300];

QBMApplePushEvent *APEvent = [QBMApplePushEvent pushEvent];
APEvent.active = NO;
APEvent.name = @"test";
APEvent.type = QBMEventTypeOneShot;
APEvent.date = fiveMinFromNow;

APEvent.isDevelopmentEnvironment = YES;

APEvent.usersIDs = [NSString stringWithFormat:@"%d", userID];


NSString *message = @"Testing APNS!";
NSMutableDictionary *payload = [NSMutableDictionary dictionary];
NSMutableDictionary *aps = [NSMutableDictionary dictionary];
[aps setObject:@"default" forKey:QBMPushMessageSoundKey];
[aps setObject:message forKey:QBMPushMessageAlertKey];
[payload setObject:aps forKey:QBMPushMessageApsKey];


QBMPushMessage *Pushmessage = [[QBMPushMessage alloc] initWithPayload:payload];



APEvent.pushMessage = Pushmessage;

[QBMessages createEvent:APEvent delegate:self];



[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

[messageBody resignFirstResponder];

但是什么也没发生,我在 NSLog 中收到“UTF-8 中的无效字节序列”错误。

以下是日志的摘录:

2014-04-28 11:32:52.508 Messages[1763:4603] +[QBMEvent messageToString:] -> message: 
payload = "\"aps\":\"sound\":\"default\",\"alert\":\"TestingAPNS\"";

2014-04-28 11:32:52.510 Messages[1763:4603] Performing async request: 

POST https://api.quickblox.com/events.xml

headers:
    "QB-SDK" = "ios 1.8.4";
    "Qb-Token" = 33360a8735e91da82e588ffdbd5cd462b52420cf;
    "QuickBlox-REST-API-Version" = "0.1.1";


parameters:
    "event[date]" = 1398677872;
    "event[environment]" = development;
    "event[event_type]" = "one_shot";
    "event[message]" =     "payload=eyJhcHMiOnsic291bmQiOiJkZWZhdWx0IiwiYWxlcnQiOiJUZXN0aW5nQVBOUyJ9fQ==";
    "event[name]" = test;
    "event[notification_type]" = push;
    "event[user][ids]" = 1026331;

2014-04-28 11:32:52.700 Messages[1763:1903] Request finished, response:

headers:
    "Access-Control-Allow-Origin" = "*";
    "Cache-Control" = "no-cache";
    Connection = "keep-alive";
    "Content-Type" = "application/xml; charset=utf-8";
    Date = "Mon, 28 Apr 2014 09:32:53 GMT";
    "QB-Token-ExpirationDate" = "2014-04-28 11:32:43 UTC";
    "QuickBlox-REST-API-Version" = "0.1.1";
    Server = "nginx/1.0.15";
    Status = "422 Unprocessable Entity";
    "Transfer-Encoding" = Identity;
    "X-Rack-Cache" = "invalidate, pass";
    "X-Request-Id" = b65b8c46d1bd4c316e0758f047f1f1b1;
    "X-Runtime" = "0.076002";
    "X-UA-Compatible" = "IE=Edge,chrome=1";


body:

error:

<?xml version="1.0" encoding="UTF-8"?>
<errors type="array">
  <error>
     <code nil="true"/>
     <message>invalid byte sequence in UTF-8</message>
  </error>
 </errors>

非常感谢您的帮助!

亲切的问候,

卢伦斯

【问题讨论】:

【参考方案1】:

您应该再添加一个参数并更改 APEvent.type:

APEvent.pushType = QBMPushTypeAPNS;
APEvent.type = QBMEventTypeFixedDate;

试试这个,希望对你有帮助

更多关于日程推送的信息请阅读此处http://quickblox.com/developers/Messages#Create_event

【讨论】:

【参考方案2】:

我得到了使用 Quickblox 的内容可用标志

我使用了 Louwren (user3580713) 问题中的以下代码:

NSString *message = @"Testing APNS!";
NSMutableDictionary *payload = [NSMutableDictionary dictionary];
NSMutableDictionary *aps = [NSMutableDictionary dictionary];
[aps setObject:@"default" forKey:QBMPushMessageSoundKey];
[aps setObject:message forKey:QBMPushMessageAlertKey];
[payload setObject:aps forKey:QBMPushMessageApsKey];


QBMPushMessage *Pushmessage = [[QBMPushMessage alloc] initWithPayload:payload];

并在 setObject:message 之后添加以下行

[aps setObject:@"1" forKey:@"content-available"];

didReceiveRemoteNotification:fetchCompletionHandler 现在在 iPhone 处于睡眠状态时被调用。我正在寻找一种添加内容可用标签的方法,这个问题帮助我弄清楚了。谢谢。希望这对某人有所帮助。

【讨论】:

以上是关于使用 quickblox 和 IOS sdk 实现预定的推送通知的主要内容,如果未能解决你的问题,请参考以下文章

QuickBlox iOS SDK 更新消息

Quickblox:即使发送消息,iOS sdk 也会在发送时返回错误

Quickblox + Swift 3.0 iOS SDK 错误:未找到应用程序和 QBConnectionZoneTypeProduction

iOS QuickBlox SDK 按用户 ID 过滤自定义对象

Quickblox:如何在 iOS SDK 中访问自定义对象特殊参数(增量)?

Quickblox IOS SDK 2.6.2 问题