• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 19:11:06 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

要订阅的iOS App代码如下:

- (void)loginAndConnectToChatQBUserWithLoginNameNSString *)loginName passwordNSString *)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] respondsToSelectorselector(registerUserNotificationSettings]) {

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

-(void)applicationUIApplication *)application didRegisterForRemoteNotificationsWithDeviceTokenNSData *)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
    }];
}

创建订阅成功响应如下:

[QBResponse], status: 201

从 JavaScript 发送推送通知的代码如下:

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"]}"}

请建议我在代码中做错了什么。



Best Answer-推荐答案


为了发送推送,您必须充当用户

为此,您必须与用户创建 session

所以你的代码应该是这样的:

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

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

关于javascript - 无法从 Web(JavaScript) QuickBlox 发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36308353/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap