doumu9019 2015-10-21 09:07
浏览 76
已采纳

我的有效负载是否格式错误,在后台调用didReceiveRemoteNotification?

According to these (and many more) questions on SO:

I figured out, that the main keys for silent push-notification and for the method

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler


to be called in the background are: the payload, iOS version > iOS 7, enabled background mode with "Remote notifications" and registering push notifications in
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method.

I did all of those things, but the method mentioned above, that supposed to be called in the background, is only being called while the app is foreground. This is how I form my payload in php:

$contents = json_encode(array(
            'aps'   => array(
                'content-available' => '1',
                'sound' => '',
                'alert' => '',
            ),
            'where' => array(
                'deviceType'    => array(
                '$in'   =>  array('ios', 'android'),
                ),
            ),
            'data'  => array(
                'title' => $this->subject,
                'msg'   => $this->message,
                'date'  => date('d.m.Y H:i', time() + (60 * 60 * 2)),
                'key'   => APPKEY,
                'ids'   => $ids,
            ),

        ));

and this is what I get in Xcode console, as I am able to NSLog the payload when app is in foreground:

{
    aps =     {
    };
    date = "21.10.2015 11:01";
    ids =     (
        259,
        257,
        256,
    );
    key = xpy3fq4t3wn9;
    msg = test11;
    title = aatest1;
}

What am I doing wrong? Why isnt "content-available = 1" in Xcode console? Or is there any other reason why appDelegate method isnt being called in background?

Im using iOS 8.1.3, Parse as Push-Notification provider.

  • 写回答

1条回答 默认 最新

  • doubi4340 2015-10-21 13:26
    关注

    I was able to solve my problem just by trying different payload combinations, ended up with this one:

    $contents = json_encode(array(
    
                'where' => array(
                    'deviceType'    => array(
                    '$in'   =>  array('ios', 'android'),
                    ),
                ),
                'data'  => array(
                    'title' => $this->subject,
                    'msg'   => $this->message,
                    'date'  => date('d.m.Y H:i', time() + (60 * 60 * 2)),
                    'key'   => APPKEY,
                    'ids'   => $ids,
                    'aps'   => array(
                         'content-available'    => '1',
                         'sound'    => '',
                         'alert' => '',
                     ),
                ),
    
            ));
    

    Looks like 'aps' has to be in 'data' array. Now the app delegate method is being called in background and I can see "content-available = 1" in Xcode console.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘