如何在收到推送通知后立即播放声音? [英] How do i get my push notification to play a sound as soon as it is received?
本文介绍了如何在收到推送通知后立即播放声音?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
App A 正在向 App B 推送通知,如下所示:
NSDictionary *data = @{@"alert" : @"购买成功!1 移除广告",@"徽章" : @"增量",@"sounds" : @"Bell.caf",};PFPush *push = [[PFPush alloc] init];[push setChannels:@[ @"Mets" ]];[push setData:data];[push sendPushInBackground];
App B 像这样接收推送通知:
<代码>////ParseStarterProjectAppDelegate.m////版权所有 2011 年至今 Parse Inc. 保留所有权利.//#import//如果要使用任何 UI 组件,请取消注释此行//#import //如果您使用的是 Facebook,请取消注释此行//#import //如果您想使用崩溃报告 - 取消注释此行//#import #import "ParseStarterProjectAppDelegate.h"#import "ParseStarterProjectViewController.h"@implementation ParseStarterProjectAppDelegate#pragma mark -#pragma mark UIApplicationDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {//启用从本地数据存储区存储和查询数据.如果您不想,请删除此行//使用本地数据存储功能或想要使用 cachePolicy.[解析 enableLocalDatastore];//************************************************************************************//如果要启用崩溃报告,请取消注释此行//[ParseCrashReporting 启用];////取消注释并填写您的 Parse 凭据:[解析 setApplicationId:@"APPIDHERE" clientKey:@"CLIENTKEYHERE"];////如果你使用 Facebook,取消注释并将你的 FacebookAppID 添加到你的包的 plist 中//此处描述:https://developers.facebook.com/docs/getting-started/facebook-sdk-for-ios///[PFFacebookUtils 初始化Facebook];//************************************************************************************[PFUser enableAutomaticUser];PFACL *defaultACL = [PFACL ACL];//如果您希望所有对象默认都是私有的,请删除此行.[defaultACL setPublicReadAccess:YES];[PFACL setDefaultACL:defaultACL withAccessForCurrentUser:YES];//在应用程序启动后覆盖自定义点.self.window.rootViewController = self.viewController;[self.window makeKeyAndVisible];如果(application.applicationState != UIApplicationStateBackground){//如果我们通过推送启动,则跟踪此处打开的应用程序,除非//"content_available" 用于触发后台推送(在 iOS 7 中引入).//在这种情况下,我们在这里跳过跟踪以避免重复计算应用程序打开.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如果([应用程序响应选择器:@选择器(注册用户通知设置:)]){UIUserNotificationType userNotificationTypes = (UIUserNotificationTypeAlert |UIUserNotificationTypeBadge |UIUserNotificationTypeSound);UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:userNotificationTypes类别:无];[应用注册用户通知设置:设置];[应用 registerForRemoteNotifications];} 别的#万一{[应用 registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeAlert |UIRemoteNotificationTypeSound)];}返回是;}#pragma mark 推送通知- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {PFInstallation *currentInstallation = [PFInstallation currentInstallation];[当前安装 setDeviceTokenFromData:deviceToken];[当前安装 saveInBackground];[PFPush subscribeToChannelInBackground:@"Mets" block:^(BOOL 成功,NSError *error) {如果(成功){NSLog(@"ParseStarterProject 成功订阅广播频道推送通知.");} 别的 {NSLog(@"ParseStarterProject 订阅广播频道推送通知失败.");}}];}- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {如果(错误代码== 3010){NSLog(@"iOS模拟器不支持推送通知.");} 别的 {//显示一些警报或以其他方式处理注册失败.NSLog(@"application:didFailToRegisterForRemoteNotificationsWithError: %@", error);}}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {[PFPush handlePush:userInfo];如果(application.applicationState == UIApplicationStateInactive){[PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];}}/////////////////////////////////////////////////////////////如果您想使用带有后台应用程序刷新的推送通知,请取消注释此方法/////////////////////////////////////////////////////////////- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {//if (application.applicationState == UIApplicationStateInactive) {//[PFAnalytics trackAppOpenedWithRemoteNotificationPayload:userInfo];//}//}#pragma mark Facebook SDK 集成/////////////////////////////////////////////////////////////如果您使用的是 Facebook,请取消注释此方法/////////////////////////////////////////////////////////////- (BOOL)application:(UIApplication *)application//openURL:(NSURL *)url//sourceApplication:(NSString *)sourceApplication//注释:(id)注释{//返回 [PFFacebookUtils handleOpenURL:url];//}@结尾
为什么 App B 收到推送通知时我没有听到任何声音?当应用程序关闭并出现横幅通知时不播放声音(我想要声音),当应用程序打开时不播放声音并且出现通知(只是振动,这很奇怪).为什么会这样,我该如何解决?
解决方案
这样做:
NSDictionary *data = @{@"alert" : @"你每天的奶温",@"徽章" : @"增量",@"声音":@"super.caf"};PFPush *pusher = [[PFPush alloc] init];[pusher setChannels:@[ @"deviceTemperatureReading" ]];[pusher setData:data];[pusher sendPushInBackground];
<块引用>
super.caf"是一个声音文件
App A is pushing a notification to App B like so:
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 Receives the push notification like so:
//
// 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
How come I don't hear any sounds go off when App B receive's the push notification? No sound is played when the app is closed and the banner notification appears (which I'd like a sound), no sound is played when the app is open and the notification appears (just a vibrate, that's odd). Why is that and how do I fix it?
解决方案
Do it like this:
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" is a sound file
这篇关于如何在收到推送通知后立即播放声音?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文