Tom*_*ool 0 objective-c push-notification apple-push-notifications ios parse-platform
场景 =我有一个具有消息传递功能的应用程序.在发送消息时,还将向接收者的电话发送推送通知.我希望用户每当用户接收带有"警报"(在屏幕上显示消息),"徽章"(红色圆圈中的应用程序图标徽章编号)和"声音"(在推送时播放的声音)的推送通知是不是看Instant Messenger的屏幕-当用户IS的即时通讯屏幕上,我希望用户收到"徽章"和"声音" ONLY.
为什么我不想在Instant Messenger屏幕上发送"警报"? 因为当消息加载到tableView时,即时消息屏幕已经向用户显示消息.当他们不得不一直点击它以便在屏幕上再次阅读时,我为什么要将IM推送到他们的屏幕上并显示"警报"?没有意义.
为什么我只希望用户在IM屏幕上收到"徽章"? 这是因为我的应用程序不断检查要增加的应用程序的徽章计数,以便在引发应用程序徽章时刷新表格视图.我还希望播放"声音"以通过声音通知用户Push已进入他们的手机.
需要注意的事项 =我的应用程序基于一个架构,其中包含一个Tab栏控制器,其中包含每个选项卡的导航控制器.如果这有所作为.
编辑 - 总结和简化的问题.
你是怎么做到的?...(不是真正的代码)
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
if (isOnInstantMessagingScreen) {
//Disable Push "Alert" messages
//Enable Push "Badge"
//Enable Push "Sound"
}
else {
//Enable Push "Alert" messages
//Enable Push "Badge"
//Enable Push "Sound"
}
Run Code Online (Sandbox Code Playgroud)
虽然应用程序处于打开状态,但Apple会使通知静音,因此您有责任显示警报或播放声音.这是个好消息,因为您不必担心取消注册通知类型.
要回答第一个问题,您可以从应用程序委托中访问topMostViewController
UIViewController *topMostViewController = [(UINavigationController *)[self.tabBarController selectedViewController] topViewController];
Run Code Online (Sandbox Code Playgroud)
我假设您已将tabBarController设置为属性
@property (strong, nonatomic) UITabBarController *tabBarController;
Run Code Online (Sandbox Code Playgroud)
如果你有超过五个选项卡,selectedViewController可以是moreViewController在这种情况下,你可以尝试
UIViewController *topMostViewController = [(UINavigationController *)[[self.tabBarController viewControllers] objectAtIndex:[self.tabBarController selectedIndex]] topViewController];
Run Code Online (Sandbox Code Playgroud)
然后,您可以指定布尔值 - MessageScreenViewController您的消息控制器在哪里
isOnInstantMessagingScreen = [topMostViewController isKindOfClass:[MessageScreenViewController class]];
Run Code Online (Sandbox Code Playgroud)
您需要知道应用程序是否在前台才能显示UIAlertView并播放声音
if([application applicationState] == UIApplicationStateActive){}
Run Code Online (Sandbox Code Playgroud)
如果用户不在消息传递屏幕上,您将需要显示通知并播放声音
if(!isOnInstantMessagingScreen){
[PFPush handlePush:userInfo]; // Display alert - can also be UIAlertView
//Play sound
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Notification" ofType:@"wav"]];
AVAudioPlayer *notify = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[notify play];
//Perhaps increment badge number
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: +1];
}
Run Code Online (Sandbox Code Playgroud)
这对你有帮助吗?
最终解决方案看起来像(未经测试):
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//The application is currently in the foreground
if([application applicationState] == UIApplicationStateActive){
UIViewController *topMostViewController = [(UINavigationController *)[self.tabBarController selectedViewController] topViewController];
BOOL isOnInstantMessagingScreen = [topMostViewController isKindOfClass:[MessageScreenViewController class]];
if(!isOnInstantMessagingScreen){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notification" message:[[userInfo objectForKey:@"aps"] objectForKey:@"alert"] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"View", nil];
alert.tag = 1;
[alert show];
//Play sound
NSURL* musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Notification" ofType:@"wav"]];
AVAudioPlayer *notify = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:nil];
[notify play];
//Perhaps increment badge number
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: +1];
}
} else {
//Application is in background - When the notification is clicked on, we will get here
[[UIApplication sharedApplication] setApplicationIconBadgeNumber: 0]; //Clear notification as we have clicked it, potentially could also be -1 to decrement?
UINavigationController *navigationController = (UINavigationController *)[self.tabBarController.viewControllers objectAtIndex:2];
BOOL isOnInstantMessagingScreen = [[navigationController topViewController] isKindOfClass:[MessageScreenViewController class]];
//Perhaps we want to navigate to the message controller screen
if(!isOnInstantMessagingScreen){
//Somehow push the messaging screen onto the UINavigationController
//This could be done here by alloc and initing the view controller
MessageScreenViewController *viewContoller = [[MessageScreenViewController alloc] init];
[navigationController pushViewController:viewContoller animated:YES];
//Or perhaps by calling a function on the rootView of the UINavigationController
RootViewController *rootViewController = (RootViewController *)[navigationController.viewControllers objectAtIndex:0];
[rootViewController loadMessageScreenWithUserInfo:userInfo];
//Show tab
[self.tabBarController setSelectedIndex:2]; // Index of NavigationController which will contain the MessageViewController
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2948 次 |
| 最近记录: |