3

嗨,我正在尝试在我的 iDevice 上收到推送通知时播放默认的推送声音我使用此代码在

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo` Method 

NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"];
    NSString *alertString =(NSString *) [test objectForKey:@"alert"];
    NSLog(@"String recieved: %@",alertString);
    if (state == UIApplicationStateActive) {
            UIAlertView *alertmessage=[[UIAlertView alloc]initWithTitle:@"iEverything Tech"
                                                                message:alertString                                                    delegate:self
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];


            [alertmessage show];

            AudioServicesPlaySystemSound(1002);


        }

        if (state == UIApplicationStateInactive) {
            AudioServicesPlaySystemSound(1002);
        }

        if (state == UIApplicationStateBackground) {
            AudioServicesPlaySystemSound(1002);
        }

我的第二个问题是如何在 AlertView 中显示 Pushed 消息?

谢谢您的回答!

而且我不能使用像 Parse 这样的推送提供程序,因为我们有自己的服务器,我们需要自动推送

4

6 回答 6

2

就像 NSEncoder 写的那样,声音必须在通知负载中。要回答第二个问题,您的通知将显示在警报、徽章或根本不显示 - 根据用户通知设置中的设置,您对此没有影响。

于 2012-11-22T13:11:32.633 回答
2

您必须使用推送通知有效负载来播放声音。阅读苹果文档。在此处输入图像描述

http://developer.apple.com/library/ios/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html#//apple_ref/doc/uid/TP40008194-CH100-SW9

于 2012-11-22T13:08:30.173 回答
1

试试这个来检索通知消息,下面的“alertString”保存收到的消息

NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"];
NSString *alertString =(NSString *) [test objectForKey:@"alert"];
NSLog(@"String recieved: %@",alertString);
于 2012-11-22T13:10:41.997 回答
0

只需将字符串传递到警报中即可

NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"];
NSString *alertString =(NSString *) [test objectForKey:@"alert"];
NSLog(@"String recieved: %@",alertString);
UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Title" message:alertString delegate:self cancelButtonTitle:@"OK" otherButtonTitles: @"Not OK", nil] autorelease];
[alert show];
于 2012-11-22T13:59:40.633 回答
0

在您的“应用程序 didfinishlaunchingwithoptions 添加以下内容

NSDictionary *remoteNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsRemoteNotificationKey];
if (remoteNotif) {
    [self handleRemoteNotification:application userInfo:remoteNotif];
    [[UIApplication sharedApplication] cancelAllLocalNotifications];
    [UIApplication sharedApplication].applicationIconBadgeNumber--;        
}

当您通过远程通知打开应用程序时,通过点击通知这将减少徽章编号,如果您想在用户打开应用程序时删除徽章编号,那么只需在 if 条件下执行代码,如果条件在这里只检查是否该应用程序已通过点击远程通知打开..,

于 2012-11-23T13:49:51.803 回答
0

为了在 iOS 中播放通知的默认声音,您需要将以下代码添加到有效负载 json

"sound" : "default"

因此,您的“通知”有效负载应类似于:

 "notification": {
        "title": "4x8",
        "body": "15:16.2342",
        "message":"https://www.google.com",
        "sound" : "default"
      }
于 2016-07-28T20:11:12.917 回答