V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
mxm145
V2EX  ›  iDev

iOS 推送问题

  •  
  •   mxm145 · 2017-06-29 10:26:53 +08:00 · 3161 次点击
    这是一个创建于 2465 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有人遇到过推送的通知测试版可以在通知栏点开到具体页面,但是 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"}}
    

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

    4 条回复    2017-10-10 11:32:18 +08:00
    q409195961
        1
    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
        2
    mxm145  
    OP
       2017-06-29 13:05:11 +08:00
    application: didFinishLaunchingWithOptions:中也是写了的,代码如下:
    ```
    NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
    [self showPushArticle:userInfo];
    }
    ```
    无论 app 在后台运行还是在前台都不会跳转,但是接收没有问题
    showme1121
        3
    showme1121  
       2017-09-30 22:30:46 +08:00 via iPhone
    确定两种版本都是通过 api 或者网页发送的数据么
    mxm145
        4
    mxm145  
    OP
       2017-10-10 11:32:18 +08:00
    @showme1121 肯定是一样的,都是使用同样的数据
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2722 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 12:38 · PVG 20:38 · LAX 05:38 · JFK 08:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.