2015-08-28 44 views
1

我很難找到有用的東西,或者我沒有正確理解,所以讓我們從頭開始。當我收到Parse的推送通知時,如何播放聲音?

應用A被通知推送,以應用B,像這樣:

NSDictionary *data = @{ 
          @"alert" : @"Purchase Successful! 1 Remove Ads", 
          @"badge" : @"Increment", 
          @"sounds" : @"Bell.caf", 
          }; 
    PFPush *push = [[PFPush alloc] init]; 
    [push setChannels:@[ @"Mets" ]]; 
    [push setData:data]; 
    [push sendPushInBackground]; 

應用B接收到像這樣的推送通知:

// 
// ParseStarterProjectAppDelegate.m 
// 
// Copyright 2011-present Parse Inc. All rights reserved. 
// 

#import <Parse/Parse.h> 

// If you want to use any of the UI components, uncomment this line 
// #import <ParseUI/ParseUI.h> 

// If you are using Facebook, uncomment this line 
// #import <ParseFacebookUtils/PFFacebookUtils.h> 

// If you want to use Crash Reporting - uncomment this line 
// #import <ParseCrashReporting/ParseCrashReporting.h> 

#import "ParseStarterProjectAppDelegate.h" 
#import "ParseStarterProjectViewController.h" 

@implementation ParseStarterProjectAppDelegate 

#pragma mark - 
#pragma mark UIApplicationDelegate 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
// Enable storing and querying data from Local Datastore. Remove this line if you don't want to 
// use Local Datastore features or want to use cachePolicy. 
[Parse enableLocalDatastore]; 

// **************************************************************************** 
// Uncomment this line if you want to enable Crash Reporting 
// [ParseCrashReporting enable]; 
// 
// Uncomment and fill in with your Parse credentials: 
[Parse setApplicationId:@"APPIDHERE" clientKey:@"CLIENTKEYHERE"]; 
// 
// If you are using Facebook, uncomment and add your FacebookAppID to your bundle's plist as 
// described here: https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios/ 
// [PFFacebookUtils initializeFacebook]; 
// **************************************************************************** 

[PFUser enableAutomaticUser]; 

PFACL *defaultACL = [PFACL ACL]; 

// If you would like all objects to be private by default, remove this line. 
[defaultACL setPublicReadAccess:YES]; 

[PFACL setDefaultACL:defaultACL withAccessForCurrentUser:YES]; 

// Override point for customization after application launch. 

self.window.rootViewController = self.viewController; 
[self.window makeKeyAndVisible]; 

if (application.applicationState != UIApplicationStateBackground) { 
    // Track an app open here if we launch with a push, unless 
    // "content_available" was used to trigger a background push (introduced in iOS 7). 
    // In that case, we skip tracking here to avoid double counting the app-open. 
    BOOL preBackgroundPush = ![application respondsToSelector:@selector(backgroundRefreshStatus)]; 
    BOOL oldPushHandlerOnly = ![self respondsToSelector:@selector(application:didReceiveRemoteNotification:fetchCompletionHandler:)]; 
    BOOL noPushPayload = ![launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey]; 
    if (preBackgroundPush || oldPushHandlerOnly || noPushPayload) { 
     [PFAnalytics trackAppOpenedWithLaunchOptions:launchOptions]; 
    } 
} 

#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000 
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) { 
    UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert | 
                UIUserNotificationTypeBadge | 
                UIUserNotificationTypeSound); 
    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes 
                      categories:nil]; 
    [application registerUserNotificationSettings:settings]; 
    [application registerForRemoteNotifications]; 
} else 
#endif 
{ 
    [application registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | 
                UIRemoteNotificationTypeAlert | 
                UIRemoteNotificationTypeSound)]; 
} 

return YES; 
} 

#pragma mark Push Notifications 

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken { 
PFInstallation *currentInstallation = [PFInstallation currentInstallation]; 
[currentInstallation setDeviceTokenFromData:deviceToken]; 
[currentInstallation saveInBackground]; 

[PFPush subscribeToChannelInBackground:@"Mets" block:^(BOOL succeeded, NSError *error) { 
    if (succeeded) { 
     NSLog(@"ParseStarterProject successfully subscribed to push notifications on the broadcast channel."); 
    } else { 
     NSLog(@"ParseStarterProject failed to subscribe to push notifications on the broadcast channel."); 
    } 
}]; 
} 

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error { 
if (error.code == 3010) { 
    NSLog(@"Push notifications are not supported in the iOS Simulator."); 
} else { 
    // show some alert or otherwise handle the failure to register. 
    NSLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error); 
} 
} 

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo { 
[PFPush handlePush:userInfo]; 

if (application.applicationState == UIApplicationStateInactive) { 
    [PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo]; 
} 
} 

/////////////////////////////////////////////////////////// 
// Uncomment this method if you want to use Push Notifications with Background App Refresh 
/////////////////////////////////////////////////////////// 
//- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { 
// if (application.applicationState == UIApplicationStateInactive) { 
//  [PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo]; 
// } 
//} 

#pragma mark Facebook SDK Integration 

/////////////////////////////////////////////////////////// 
// Uncomment this method if you are using Facebook 
/////////////////////////////////////////////////////////// 
//- (BOOL)application:(UIApplication *)application 
//   openURL:(NSURL *)url 
// sourceApplication:(NSString *)sourceApplication 
//   annotation:(id)annotation { 
// return [PFFacebookUtils handleOpenURL:url]; 
//} 

@end 

現在怎麼來的我沒有聽到任何聲音去當應用程序B收到推送通知時關閉?當應用程序關閉並且出現橫幅通知(我想要聲音)時沒有聲音,當應用程序打開並且出現通知時(只是振動,很奇怪),沒有聲音。

任何幫助將不勝感激。

謝謝

+0

您確定手機未處於靜音模式嗎? – efimovdk

+0

您是否在設備設置中檢查了推送通知的設置? – anhtu

+0

是的,手機沒有處於靜音模式,並且是,按下了推送通知設置中的聲音選項。 「/ – Creagen

回答

1

我之前沒有做過,但在解析搜索我得到了一些解決方案,嘗試這可能是幫助你

改變這一行

NSDictionary *data = @{ 
         @"alert" : @"Purchase Successful! 1 Remove Ads", 
         @"badge" : @"Increment", 
         @"sounds" : @"Bell.caf", 
         }; 

NSDictionary *data = @{ 
         @"alert" : @"Purchase Successful! 1 Remove Ads", 
         @"badge" : @"Increment", 
         @"sound" : @"Bell.caf", --> the key Name is not sounds 
         }; 

參考link

+0

其他嘗試一次聲音 - >默認,如果它的工作檢查那個聲音文件 –

1
NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys: 
    @"This is an alert with a custom horn sound!", @"alert", 
    @"Increment", @"badge", 
    @"homerun.caf", @"sound", 
    @"5", @"score_REDSOX", 
    @"0", @"score_YANKEES", 
    @"4th", @"inning", 
    nil]; 

PFPush *push = [[PFPush alloc] init]; 
[push setChannels:[NSArray arrayWithObjects:@"REDSOX", @"YANKEES", nil]]; 
[push setPushToAndroid:false]; 
[push expireAfterTimeInterval:86400]; 
[push setData:data]; 
[push sendPushInBackground]; 



Push Notifications Web Console 
{ 
    "alert": "This is an alert with a custom horn sound!", 
    "badge": "Increment", 
    "sound": "homerun.caf", 
    "score_REDSOX": "5", 
    "score_YANKEES": "0", 
    "inning": "4th" 
} 
1

我已通過PHP代碼發送Apple推送通知。

$body['aps'] = array(
'alert' => 'Hi hello message......', 
'sound' => 'Bell.caf', 
'badge' => '1', 
); 

可能這會幫助你。

相關問題