菜鸟教程小白 发表于 2022-12-12 19:11:06

javascript - 无法从 Web(JavaScript) QuickBlox 发送推送通知


                                            <p><p>我正在尝试使用 QuickBlox 推送通知 API 从 Web 后端 (JavaScript) 向 iOS 设备发送推送通知。我在 iOS 应用上创建了订阅,并尝试从 JavaScript 向订阅用户发送推送通知。</p>

<p>要订阅的iOS App代码如下:</p>

<pre><code>- (void)loginAndConnectToChatQBUserWithLoginName:(NSString *)loginName password:(NSString *)password{
    isConnecting = YES;

    QBUUser *user = [ 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(@&#34;Logged in&#34;);
      ;

      .currentUser = user;

      user.password = password;

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

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

- (void)registerForRemoteNotifications{

#if __IPHONE_OS_VERSION_MAX_ALLOWED &gt;= 80000
    if ([ respondsToSelector:@selector(registerUserNotificationSettings:)]) {

      [ registerUserNotificationSettings:];
      [ registerForRemoteNotifications];
    }
    else{
      [ registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
    }
#else
    [ registerForRemoteNotificationTypes:UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound];
#endif
}

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

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

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

    } errorBlock:^(QBResponse *response) {
      //Subscription Failure
    }];
}
</code></pre>

<p>创建订阅成功响应如下:</p>

<pre><code>, status: 201
</code></pre>

<p>从 JavaScript 发送推送通知的代码如下:</p>

<pre><code>var pushCustomParams = {
message: &#39;Message received from Bob&#39;,
ios_badge: 1,
ios_sound: &#39;mysound.wav&#39;, //Sound File name
user_id: &#39;234&#39;, //Caller User ID
}

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

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

<p>在创建事件时我一直收到错误:</p>

<pre><code> create Object { notification_type:&#34;push&#34;, user:Object,environment:&#34;development&#34;,message:&#34;ey.....&#34;}

Request: POST Object {data:&#34;{&#34;url&#34;:&#34;https://api.quickblox.com/e..&#34;}

ajax error 401 Unauthorized {&#34;errors&#34; : [&#34;Token is required&#34;]}

Object {code:401, status:&#34;error&#34;, message:&#34;Unauthorized&#34;, detail:&#34;{errors&#34;[&#34;Token is required&#34;]}&#34;}
</code></pre>

<p>请建议我在代码中做错了什么。</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>为了发送推送,您必须充当用户</p>

<p>为此,您必须与用户创建 session </p>

<p>所以你的代码应该是这样的:</p>

<pre><code>var params = {login: &#39;garry&#39;, password: &#39;garry5santos&#39;};

QB.createSession(params, function(err, result) {
if(!err){
    // send a push code
}
});
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于javascript - 无法从 Web(JavaScript) QuickBlox 发送推送通知,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/36308353/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/36308353/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: javascript - 无法从 Web(JavaScript) QuickBlox 发送推送通知