如何让我的推送通知在收到后立即播放声音?
Posted
技术标签:
【中文标题】如何让我的推送通知在收到后立即播放声音?【英文标题】:How do i get my push notification to play a sound as soon as it is received? 【发布时间】:2015-08-28 01:13:01 【问题描述】:应用 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];
App 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
App B 收到推送通知时,为什么我没有听到任何声音?关闭应用程序并出现横幅通知时不播放声音(我想要声音),打开应用程序并出现通知时不播放声音(只是振动,这很奇怪)。为什么会这样,我该如何解决?
【问题讨论】:
No Sound in ios 8 Parse push的可能重复 【参考方案1】:这样做:
NSDictionary *data = @
@"alert" : @"you daily milk temp",
@"badge" : @"Increment",
@"sound" : @"super.caf"
;
PFPush *pusher = [[PFPush alloc] init];
[pusher setChannels:@[ @"deviceTemperatureReading" ]];
[pusher setData:data];
[pusher sendPushInBackground];
“super.caf”是一个声音文件
【讨论】:
我是否只是将 super.caf 添加到 App B 并且当 App A 发送该推送时,App B 应该播放该声音? 如果两个用户在应用程序包中都有这个,那么这意味着任何得到推送的人都会听到声音。另外,顺便说一下,您应该将您的问题标记为已回答或未回答 两个应用程序都有音频文件,但仍然没有声音。两个应用都必须有 AVFoundation 吗? k,这对我有用,我没有你的代码,我不知道你在做什么,所以也许其他人可以向你展示相同的代码然后它会工作? 没有更多的答案,我已经回答完了你的问题,谢谢以上是关于如何让我的推送通知在收到后立即播放声音?的主要内容,如果未能解决你的问题,请参考以下文章