-1

当我开发一个要求用户允许推送通知的 iOS 应用程序时,我想到了这个疑问。如果用户回答“是”,则其设备“push id”将发送到我们的网络服务器,并将其存储在 db 表中。每次我们想向他发送消息时,我们都使用此 id 与苹果服务通信,该服务实际上将其发送到他的设备;如果他决定停止它们,他可以从 iOS 设置页面中选择性地仅针对我们的应用禁用通知。

完成此操作后我们不会收到任何信息,因此我们的服务器不会收到任何通知:它会继续向苹果发送消息,认为它们已送达。

我的问题:当用户从设置中禁用某个应用程序的通知时(在这种情况下:我们可以知道它,以节省带宽跳过不想被“推送”的用户吗?不再),还是iOS只是拒绝(隐藏?)用户已接受然后禁用的应用程序的推送通知?

如果最后一种情况属实,我们可以认为,随着用户在设备的生命周期内安装大量应用程序,电池寿命将会缩短,因为越来越多的推送通知被远程接收并被本地忽略(...即使旧的应用程序被卸载??)

4

2 回答 2

1

来自 Apple 的本地和推送通知编程指南

Apple 推送通知服务包括一项反馈服务,可为您提供有关失败推送通知的信息。当由于设备上不存在预期的应用程序而无法传递推送通知时,反馈服务会将该设备的令牌添加到其列表中。在传递之前过期的推送通知不被视为失败传递,并且不会影响反馈服务。通过使用此信息停止发送无法传递的推送通知,您可以减少不必要的消息开销并提高整体系统性能。

每天查询反馈服务以获取设备令牌列表。使用时间戳来验证设备令牌在生成反馈条目后没有被重新注册。对于尚未重新注册的每台设备,停止发送通知。APNs 监控提供商在检查反馈服务和避免向设备上不存在的应用程序发送推送通知方面的努力。

于 2013-07-19T10:40:20.030 回答
-2

您可以有条件地执行以下操作:

在 AppDelegate.m

#pragma mark PUSH NOTIFICATION

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)token
{
    NSUInteger rntypes = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
    // Set the defaults to disabled unless we find otherwise...
    NSString *pushBadge = @"disabled";
    NSString *pushAlert = @"disabled";
    NSString *pushSound = @"disabled";

    if(rntypes == UIRemoteNotificationTypeBadge)
    {
        pushBadge = @"enabled";
    }
    else if(rntypes == UIRemoteNotificationTypeAlert)
    {
        pushAlert = @"enabled";
    }
    else if(rntypes == UIRemoteNotificationTypeSound)
    {
        pushSound = @"enabled";
    }
    else if(rntypes == ( UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert))
    {
        pushBadge = @"enabled";
        pushAlert = @"enabled";
    }
    else if(rntypes == ( UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound))
    {
        pushBadge = @"enabled";
        pushSound = @"enabled";
    }
    else if(rntypes == ( UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound))
    {
        pushAlert = @"enabled";
        pushSound = @"enabled";
    }
    else if(rntypes == ( UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeSound))
    {
        pushBadge = @"enabled";
        pushAlert = @"enabled";
        pushSound = @"enabled";
    }

    NSLog(@"PUSH SOUND %@",pushBadge);
    NSLog(@"PUSH ALERT %@",pushAlert);
    NSLog(@"PUSH SOUND %@",pushSound);

    NSString *deviceToken = [[[[token description]
                               stringByReplacingOccurrencesOfString:@"<"withString:@""]
                              stringByReplacingOccurrencesOfString:@">" withString:@""]
                             stringByReplacingOccurrencesOfString: @" " withString: @""];

    NSLog(@"%d bytes", [token length]);
    NSLog(@"device token = %@", deviceToken);
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
    NSString *str1 = [NSString stringWithFormat: @"Error: %@", err];
    NSLog(@"%@",str1);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    for (id key in userInfo)
    {
        NSLog(@"key: %@, value: %@", key, [userInfo objectForKey:key]);
    }

    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);

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

    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Notification" message:alert delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

并将其放入 didFinishLaunchingWithOptions 中:

 [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];
    [UIApplication sharedApplication].applicationIconBadgeNumber = 0;

并且不要将声音与您的通知一起发送!!!

于 2013-07-19T07:25:53.867 回答