2016-03-30 15 views
1

QuickBlox Push Notification APIを使用してWebバックエンド(JavaScript)からプッシュ通知をiOSデバイスに送信しようとしています。私はiOSアプリケーションのサブスクリプションを作成し、JavaScriptから購読しているユーザーにプッシュ通知を送信しようとしています。次のようにWebからPush通知を送信できません(JavaScript)QuickBlox

購読するiOSのアプリのコードは次のとおりです。

- (void)loginAndConnectToChatQBUserWithLoginName:(NSString *)loginName password:(NSString *)password{ 
    isConnecting = YES; 

    QBUUser *user = [[QBUUser alloc] init]; 
    user.login = loginName; 
    user.password = password; 

    __weak typeof(self) weakSelf = self; 

    //Authenticate user 

    [QBRequest logInWithUserLogin:user.login password:user.password successBlock:^(QBResponse * _Nonnull response, QBUUser * _Nullable user) { 
     NSLog(@"Logged in"); 
     [weakSelf registerForRemoteNotifications]; 

     [AppDelegate appDelegate].currentUser = user; 

     user.password = password; 

     [QBChat.instance connectWithUser:user completion:^(NSError * _Nullable error) { 
      if (QBChat.instance.isConnected) { 
       NSLog(@"chat is connected"); 
      } 
      else{ 
       NSLog(@"chat not connected"); 
      } 

      isConnecting = NO; 
     }]; 
    } errorBlock:^(QBResponse * _Nonnull response) { 
     // Handle error here 
     NSLog(@"Unable to connect"); 
     isConnecting = NO; 
    }]; 
} 

- (void)registerForRemoteNotifications{ 

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(registerUserNotificationSettings:)]) { 

     [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:(UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge) categories:nil]]; 
     [[UIApplication sharedApplication] registerForRemoteNotifications]; 
    } 
    else{ 
     [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; 
    } 
#else 
    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound]; 
#endif 
} 

-(void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ 
    NSString *deviceIdentifier = [[[UIDevice currentDevice] identifierForVendor] UUIDString]; 

    QBMSubscription *subscription = [QBMSubscription subscription]; 
    subscription.notificationChannel = QBMNotificationChannelAPNS; 
    subscription.deviceUDID = deviceIdentifier; 
    subscription.deviceToken = deviceToken; 

    [QBRequest createSubscription:subscription successBlock:^(QBResponse *response, NSArray *objects) { 
     //Subscription Successfull 
     NSLog(@"Subscription Successfull."); 

    } errorBlock:^(QBResponse *response) { 
     //Subscription Failure 
    }]; 
} 

は、サブスクリプションの成功応答を作成し、以下の通りである:以下のように、JavaScriptからのプッシュ通知を送信するために

[QBResponse], status: 201 

コードは次のとおりです。

var pushCustomParams = { 
    message: 'Message received from Bob', 
    ios_badge: 1, 
    ios_sound: 'mysound.wav', //Sound File name 
    user_id: '234', //Caller User ID 
} 

var params = { 
    notification_type: 'push', 
    user: {ids: [110678]}, // recipients. 
    environment: 'development', // environment, can be 'production' as well. 
    message: QB.pushnotifications.base64Encode(JSON.stringify(pushCustomParams)) 
}; 

QB.pushnotifications.events.create(params, function(err, response) { 
    if (err) { 
    console.log(err); 
    } else { 
    // success 
    } 
}); 

イベントで作成すると、常にエラーが発生します。

[EventsProxy] create Object { notification_type:"push", user:Object,environment:"development",message:"ey....."} 

[ServiceProxy] Request: POST Object {data:"{"url":"https://api.quickblox.com/e.."} 

[ServiceProxy] ajax error 401 Unauthorized {"errors" : ["Token is required"]} 

Object {code:401, status:"error", message:"Unauthorized", detail:"{errors"["Token is required"]}"} 

私がコードで間違っていることを教えてください。あなたがユーザーとのセッションを作成する必要がこれを行うには、ユーザー

として行動しなければならないプッシュを送信するために

答えて

2

ので、あなたのコードは、そのようにする必要があります:

var params = {login: 'garry', password: 'garry5santos'}; 

QB.createSession(params, function(err, result) { 
    if(!err){ 
    // send a push code 
    } 
}); 
+0

ありがとう@lgor、それは働いた。 –

+0

Web上でエラーが発生せず、通知もquickbloxプッシュのリストにキューイングされますが、デバイス上の通知はありません。私はウェブコンソールから送信するとプッシュを受け取ることができます。次の作業を提案してください。 –

+0

@RaviGupta iOSアプリで正しいものをすべて設定していることを確認してください:http://quickblox.com/developers/SimpleSample-messages_users-ios –

関連する問題