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

iphone - 重定向 UIAlertView 按钮推送通知

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

我已经阅读了很多关于这个问题的帖子,但没有一个能真正帮助我。这似乎很容易做到,但不知何故我无法弄清楚。

我想在这里做什么? 好吧,我向我的应用程序发送推送通知。一切正常。 我可以在它处于后台、非事件状态甚至应用程序未运行时处理它。 但是另一种状态——前台应用程序——让我头疼。

我有这个正在使用的代码片段。一切都好。 当应用程序在前台运行时,用户会获得 UIAlertview。 但是如何向 View 按钮添加操作? 我的意思是这个特定的 Action :当有人点击查看按钮时,他/她会被重定向到通过推送通知传递的 url。

就像我之前在代码片段中使用的一样(准确地说是这部分):

NSString *notifUrl = [apsInfo objectForKey"url"];
NSLog(@"Received Push Url: %@", notifUrl);

我希望这是有道理的,有人可以帮助我。我迷失了这个......这是与这个主题相关的整个代码片段。

-(void)RedirectNSString*)url{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    NSLog(@"%@",url);
}     

/**
     * Remote Notification Received while application was open.
     */
    - (void)applicationUIApplication *)application didReceiveRemoteNotificationNSDictionary *)userInfo {

    #if !TARGET_IPHONE_SIMULATOR

        NSLog(@"remote notification: %@",[userInfo description]);
        NSDictionary *apsInfo = [userInfo objectForKey"aps"];

        NSString *alert = [apsInfo objectForKey"alert"];
        NSLog(@"Received Push Alert: %@", alert);

        NSString *sound = [apsInfo objectForKey"sound"];
        NSLog(@"Received Push Sound: %@", sound);
        AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

        NSString *badge = [apsInfo objectForKey"badge"];
        NSLog(@"Received Push Badge: %@", badge);
        application.applicationIconBadgeNumber = [[apsInfo objectForKey"badge"] integerValue];

        NSString *notifUrl = [apsInfo objectForKey"url"];
        NSLog(@"Received Push Url: %@", notifUrl);

        // app was already in the foreground
        if ( application.applicationState == UIApplicationStateActive ) {

            UIAlertView *alertPush = [[UIAlertView alloc]initWithTitle: @"Message"
                                                           message: alert
                                                          delegate: self
                                                 cancelButtonTitle"View"
                                                 otherButtonTitles: @"Cancel", nil];

            [alertPush show];
            [alertPush release];
        }
        // app is inactive or in the background
        else {
            [self Redirect:notifUrl];
        }

    #endif
    }

    // what to do if user taps the viewbutton in push notification alert view
    - (void)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)buttonIndex {
        if(buttonIndex == 0) {

            NSLog(@"View Button has been tapped");
            // We need the url that has been passed by the push notification


        }
        else {
            // Do something
        }
    }



Best Answer-推荐答案


只需在 .h 文件中定义您的变量 notifUrl ,以便您也可以在类的其他方法中访问它。然后将 url 存储在该变量中并在 alertview 委托(delegate)方法中使用它。

//Add this in .h file
NSString *notifUrl;

//Add this in .m file

-(void)RedirectNSString*)url{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    NSLog(@"%@",url);
}     

/**
     * Remote Notification Received while application was open.
     */
    - (void)applicationUIApplication *)application didReceiveRemoteNotificationNSDictionary *)userInfo {

    #if !TARGET_IPHONE_SIMULATOR

        NSLog(@"remote notification: %@",[userInfo description]);
        NSDictionary *apsInfo = [userInfo objectForKey"aps"];

        NSString *alert = [apsInfo objectForKey"alert"];
        NSLog(@"Received Push Alert: %@", alert);

        NSString *sound = [apsInfo objectForKey:@"sound"];
        NSLog(@"Received Push Sound: %@", sound);
        AudioServicesPlayAlertSound(kSystemSoundID_Vibrate);

        NSString *badge = [apsInfo objectForKey:@"badge"];
        NSLog(@"Received Push Badge: %@", badge);
        application.applicationIconBadgeNumber = [[apsInfo objectForKey:@"badge"] integerValue];

        //Define this variable in .h file so that you can access this in other methods as well
        notifUrl = [apsInfo objectForKey:@"url"];
        NSLog(@"Received Push Url: %@", notifUrl);
        [notifUrl retain];    // this is retain value of notifUrl so that you can use it later
        // app was already in the foreground
        if ( application.applicationState == UIApplicationStateActive ) {

            UIAlertView *alertPush = [[UIAlertView alloc]initWithTitle: @"Message"
                                                           message: alert
                                                          delegate: self
                                                 cancelButtonTitle:@"View"
                                                 otherButtonTitles: @"Cancel", nil];

            [alertPush show];
            [alertPush release];
        }
        // app is inactive or in the background
        else {
            [self Redirect:notifUrl];
        }

    #endif
    }

    // what to do if user taps the viewbutton in push notification alert view
    - (void)alertViewUIAlertView *)alertView clickedButtonAtIndexNSInteger)buttonIndex {
        if(buttonIndex == 0) {

            NSLog(@"View Button has been tapped");
            NSLog(@"Received Push Url: %@", notifUrl);
           [self Redirect:notifUrl];
        }
        else {
            // Do something
        }
    }

关于iphone - 重定向 UIAlertView 按钮推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18764038/

回复

使用道具 举报

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

本版积分规则

关注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