2012-11-22 57 views
3

嗨,我嘗試播放默認推送聲音在我的iDevice接收推送通知時,我用這個代碼在如何在iPhone接收到推送通知時

-(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中顯示推送的消息?

謝謝你的回答!

我不能使用推送提供商像解析,因爲我們小時自己的服務器,我們需要推動自動

回答

1

嘗試使用此方法檢索該通知消息,「alertString」下面保存消息收到

NSDictionary *test =(NSDictionary *)[userInfo objectForKey:@"aps"]; 
NSString *alertString =(NSString *) [test objectForKey:@"alert"]; 
NSLog(@"String recieved: %@",alertString); 
+0

感謝您的Answet,但我喜歡在AlertView中查看消息,而不是在登錄Xcode中使用此功能,但之後應用程序崩潰。檢查我添加的問題。 –

+0

好的,謝謝我解決了你的帖子,坦克的問題!我將代碼添加到我的問題 –

2

像NSEncoder寫道,聲音必須是在通知有效載荷。要回答第二個問題,您的通知將顯示在警報,徽章中或根本不顯示 - 取決於用戶通知設置中的設置,您對此沒有任何影響。

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]; 
+0

好的,謝謝你太棒了,你幫了我很多! –

+0

另一個問題,你知道如何刪除獾當我打開應用程序?我試過這個[UIApplication sharedApplication] .applicationIconBadgeNumber = 0;但它根本不起作用。 –

0

在你的「應用程序didfinishlaunchingwithoptions添加以下

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

當您通過遠程通知打開應用程序,通過點擊通知這會減少徽章數量,如果你想在用戶打開應用程序時刪除徽章號碼,那麼只要在if條件下實現代碼,如果這裏的條件只是通過點擊re來檢查應用程序是否已經打開微塵通知..,

0

爲了發揮默認的聲音在iOS的通知,您需要將以下代碼添加到有效載荷JSON

"sound" : "default" 

所以,你「通知」的有效載荷應該看的東西比如:

"notification": { 
     "title": "4x8", 
     "body": "15:16.2342", 
     "message":"https://www.google.com", 
     "sound" : "default" 
     }