iOS 推送问题

2017-06-29 10:26:53 +08:00
 mxm145

有人遇到过推送的通知测试版可以在通知栏点开到具体页面,但是 appstore 版本点开就停在首页的问题吗? 我的跳转代码是这样的

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    if (application.applicationState == UIApplicationStateActive) {
        [self gotoUrl:userInfo];
    }else{
        [self showPushArticle:userInfo];
    }
    completionHandler(UIBackgroundFetchResultNewData);
}

showPushArticle 函数

- (void)showPushArticle:(NSDictionary *)userInfo
{
    if (userInfo == NULL) {
        return;
    }
    
    NSDictionary *article = [userInfo objectForKey:@"ext"];
    if(article)
    {
        NSString *url  = [article objectForKey:@"l"];
        
        if(![url hasPrefix:@"https://"]){
            return;
        }
        
        PTWebViewController *viewcontroller = [[PTWebViewController alloc] init];
        viewcontroller.webURL = url;
        [viewcontroller setNavigationTitle:@"推送新闻"];
        [self.drawerController.centerNavigationController pushViewController:viewcontroller animated:YES];
    }
}

push 的 json 格式是:

{"aps":{"alert":"title","badge":1,"sound": "default"},"ext":{"l":"https://example.com"}}

不知道哪位遇到过同样的问题,麻烦指教一下

3200 次点击
所在节点    iDev
4 条回复
q409195961
2017-06-29 12:54:14 +08:00
你先确定第二种情况(App Store 版本),App 是没在后台运行的,还是在后台运行的?
如果没后台运行的,你需要从- application: didFinishLaunchingWithOptions: 中取出推送的内容
```
NSDictionary * remoteNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
```

另:
if(![url hasPrefix:@"https://"]){
建议改成
if(![url hasPrefix:@"http"]){
这样 url 是 http 也可以调起 WebView 控制器
mxm145
2017-06-29 13:05:11 +08:00
application: didFinishLaunchingWithOptions:中也是写了的,代码如下:
```
NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo) {
[self showPushArticle:userInfo];
}
```
无论 app 在后台运行还是在前台都不会跳转,但是接收没有问题
showme1121
2017-09-30 22:30:46 +08:00
确定两种版本都是通过 api 或者网页发送的数据么
mxm145
2017-10-10 11:32:18 +08:00
@showme1121 肯定是一样的,都是使用同样的数据

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/371851

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX