Just My Life & My Work

[iOS] Quickblox 推播

Quickblox是個開發通訊軟體的好平台,該有的基礎已經幫我們建置完畢,我們只要在其架構上客製想要的功能即可,就能打造出像我們台灣最夯的通訊App Line囉~

Quickblox logo

既然是通訊軟體,收發訊息就要考慮使用中與待機中,也就是app在前景與app在背景兩種情況。這裡我要提及的是推播,就是屬於app在背景狀態。

同時間iOS與Android協作,在測試推播的時候發現,A可以順利推播給i,而i卻無法推播給A!想說這會不會是Qb的bug⋯⋯後來才發現,原來是API不一樣,而官方文件也寫得很清楚XD~

Send Push Notifications from application (via API)

It’s possible to send 2 types of push notifications:

  1. Platform based Push Notifications
  2. Universal push notifications

iOS這邊使用到第一種作法,而Android官方卻沒有實作第一種作法,所以只要i和A都使用第二種做法就能互通推播囉~

接著記錄一下iOS這兩種做法⋯⋯

Platform based Push Notifications

Send Platform based push notifications (APNS) (only works for iOS mobile and Safari desktop). Platform based push notification will be delivered to specified platform only – in our case it’s iOS mobile and Safari desktop:

Send Push Notification to particular users (through their IDs)

NSString *message = @"Hello HappyMan!";
NSMutableDictionary *payload = [NSMutableDictionary dictionary];
NSMutableDictionary *aps = [NSMutableDictionary dictionary];
[aps setObject:@"default" forKey:QBMPushMessageSoundKey];
[aps setObject:message forKey:QBMPushMessageAlertKey];
[payload setObject:aps forKey:QBMPushMessageApsKey];
 
QBMPushMessage *message = [[QBMPushMessage alloc] initWithPayload:payload];
 
// Send push to users with ids 277,300,1395
[QBRequest sendPush:pushMessage toUsers:@"277,300,1395" successBlock:^(QBResponse *response, QBMEvent *event) {
    // Successful response with event
} errorBlock:^(QBError *error) {
    // Handle error
}];
Send push notification to a group of users (via Tags)

NSString *mesage = @"Hello HappyMan!";
NSMutableDictionary *payload = [NSMutableDictionary dictionary];
NSMutableDictionary *aps = [NSMutableDictionary dictionary];
[aps setObject:@"default" forKey:QBMPushMessageSoundKey];
[aps setObject:mesage forKey:QBMPushMessageAlertKey];
[payload setObject:aps forKey:QBMPushMessageApsKey];
 
QBMPushMessage *message = [[QBMPushMessage alloc] initWithPayload:payload];
 
// Send push to groups 'man' and 'car'
[QBRequest sendPush:pushMessage toUsersWithAnyOfTheseTags:@"man,car" successBlock:^(QBResponse *response, QBMEvent *event) {
    // Successful response with event
} errorBlock:^(QBError *error) {
    // Handler error
}];

Universal push notifications

Universal push notifications will be delivered to all possible platforms and devices for specified users. With universal push notifications there are 2 ways to send it:

  1. Just send a simple push with text only
  2. With custom parameters
Simple push with text:

// Send push to users with ids 277,300,1295
[QBRequest sendPushWithText:@"Hello HappyMan" toUsers:@"277,300,1295" successBlock:^(QBResponse *response, QBMEvent *event) {
    // Successful response with event    
} errorBlock:^(QBError *error) {
    // Handle error
}];
With custom parameters:

// Send push to users with ids 277,300,1295
QBMEvent *event = [QBMEvent event];
event.notificationType = QBMNotificationTypePush;
event.usersIDs = @"277,300,129";
event.isDevelopmentEnvironment = ![QBSettings isUseProductionEnvironmentForPushNotifications];
event.type = QBMEventTypeOneShot; 
//
// custom params
NSMutableDictionary  *dictPush=[NSMutableDictionary  dictionaryWithObjectsAndKeys:@"Message received from Bob", @"message", nil];
[dictPush setObject:@"5" forKey:@"ios_badge"];
[dictPush setObject:@"mysound.wav" forKey:@"ios_sound"];
[dictPush setObject:@"234" forKey:@"user_id"];
//
NSError *error = nil;
NSData *sendData = [NSJSONSerialization dataWithJSONObject:dictPush options:NSJSONWritingPrettyPrinted error:&error];
NSString *jsonString = [[NSString alloc] initWithData:sendData encoding:NSUTF8StringEncoding];
//
event.message = jsonString;
 
[QBRequest createEvent:event successBlock:^(QBResponse *response, QBMEvent *event) {
    // Successful response with event
} errorBlock:^(QBResponse *response) {
    // Handle error
}];

以上格式直接套參數使用,裝置就能收到推播囉~這裡要記住key要符合Qb的格式,比如推播聲音的key要是ios_sound而非sound喔!

話說,Qb的推播速度比Parse慢太多啦~Parse約1秒就能收到,而Qb卻可能要等30秒才會收到⋯⋯這後果就是,使用者不會想用這支appXD~

參考:Quickblox – Push Notifications

隨意留個言吧:)~

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料

標籤雲