Ab'*_*tio 171 iphone objective-c push-notification ios
我在我的应用程序中使用推送通知服务.当应用程序在后台时,我能够在通知屏幕上看到通知(当我们从iOS设备的顶部向下滑动时显示的屏幕).但是如果应用程序在前台是委托方法
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo
Run Code Online (Sandbox Code Playgroud)
正在调用,但通知屏幕中不显示通知.
我想在通知屏幕上显示通知,无论应用程序是在后台还是前台.我厌倦了寻找解决方案.任何帮助是极大的赞赏.
che*_*sam 172
要在应用程序位于前台时显示横幅消息,请使用以下方法.
iOS 10,Swift 3/4:
// This method will be called when app received push notifications in foreground
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
completionHandler([.alert, .badge, .sound])
}
Run Code Online (Sandbox Code Playgroud)
iOS 10,Swift 2.3:
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
{
//Handle the notification
completionHandler(
[UNNotificationPresentationOptions.Alert,
UNNotificationPresentationOptions.Sound,
UNNotificationPresentationOptions.Badge])
}
Run Code Online (Sandbox Code Playgroud)
您还必须将您的应用代表注册为通知中心的代表:
import UserNotifications
// snip!
class AppDelegate : UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate
// snip!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// set the delegate in didFinishLaunchingWithOptions
UNUserNotificationCenter.current().delegate = self
...
}
Run Code Online (Sandbox Code Playgroud)
Mah*_*eri 56
下面的代码将适合您:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
application.applicationIconBadgeNumber = 0;
//self.textView.text = [userInfo description];
// We can determine whether an application is launched as a result of the user tapping the action
// button or whether the notification was delivered to the already-running application by examining
// the application state.
if (application.applicationState == UIApplicationStateActive) {
// Nothing to do if applicationState is Inactive, the iOS already displayed an alert view.
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"Your App name received this notification while it was running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}
Run Code Online (Sandbox Code Playgroud)
Avi*_*oss 38
对于任何可能感兴趣的人,我最终创建了一个自定义视图,看起来像顶部的系统推送横幅,但添加了一个关闭按钮(小蓝色X)和一个选项来点击消息进行自定义操作.它还支持在用户有时间读取/解除旧通知之前到达多个通知的情况(没有限制可以堆积多少...)
用法基本上是在线:
[AGPushNoteView showWithNotificationMessage:@"John Doe sent you a message!"];
Run Code Online (Sandbox Code Playgroud)
它在iOS7上看起来像这样(iOS6具有iOS6的外观和感觉......)

Ash*_*ale 31
目标C.
因为iOS 10我们需要集成willPresentNotification显示通知横幅的方法foreground.
如果app处于前台模式(活动)
- (void)userNotificationCenter:(UNUserNotificationCenter* )center willPresentNotification:(UNNotification* )notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler {
NSLog( @“Here handle push notification in foreground" );
//For notification Banner - when app in foreground
completionHandler(UNNotificationPresentationOptionAlert);
// Print Notification info
NSLog(@"Userinfo %@",notification.request.content.userInfo);
}
Run Code Online (Sandbox Code Playgroud)
Ab'*_*tio 23
如果应用程序在前台运行,iOS将不会显示通知横幅/警报.这是设计的.但我们可以通过UILocalNotification如下使用来实现它
在收到远程
通知时检查应用程序是否处于活动状态.如果处于活动状态,则触发UILocalNotification.
if (application.applicationState == UIApplicationStateActive ) {
UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = userInfo;
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.alertBody = message;
localNotification.fireDate = [NSDate date];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
Run Code Online (Sandbox Code Playgroud)迅速:
if application.applicationState == .active {
var localNotification = UILocalNotification()
localNotification.userInfo = userInfo
localNotification.soundName = UILocalNotificationDefaultSoundName
localNotification.alertBody = message
localNotification.fireDate = Date()
UIApplication.shared.scheduleLocalNotification(localNotification)
}
Run Code Online (Sandbox Code Playgroud)
小智 14
对于 Swift 5
1) 确认委托给 AppDelegate
UNUserNotificationCenterDelegate2)
UNUserNotificationCenter.current().delegate = self在didFinishLaunch3) 在
AppDelegate.
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
print("Push notification received in foreground.")
completionHandler([.alert, .sound, .badge])
}
Run Code Online (Sandbox Code Playgroud)
就是这样!
Dan*_*tín 13
如果应用程序在前台运行,iOS将不会显示通知横幅/警报.这是设计的.您必须编写一些代码来处理应用程序在前台接收通知时的情况.您应该以最合适的方式显示通知(例如,将徽章编号添加到UITabBar图标,模拟通知中心横幅等).
您可以创建自己的模仿横幅提醒的通知.
一种方法是创建一个看起来像横幅的自定义uiview,可以设置动画并响应触摸.考虑到这一点,您可以使用更多功能创建更好的横幅.
或者你可以找一个为你做的api,并将它们作为podfiles添加到你的项目中.
这是我用过的一对夫妇:
https://github.com/terryworona/TWMessageBarManager
https://github.com/toursprung/TSMessages
以下是app处于活动状态(前台或打开)时接收推送通知的代码. UNUserNotificationCenter文档
@available(iOS 10.0, *)
func userNotificationCenter(center: UNUserNotificationCenter, willPresentNotification notification: UNNotification, withCompletionHandler completionHandler: (UNNotificationPresentationOptions) -> Void)
{
completionHandler([UNNotificationPresentationOptions.Alert,UNNotificationPresentationOptions.Sound,UNNotificationPresentationOptions.Badge])
}
Run Code Online (Sandbox Code Playgroud)
如果您需要访问userInfo的通知使用代码: notification.request.content.userInfo
Xcode 10 Swift 4.2
要在您的应用程序位于前台时显示推送通知-
步骤1:在AppDelegate类中添加委托UNUserNotificationCenterDelegate。
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
Run Code Online (Sandbox Code Playgroud)
步骤2:设置UNUserNotificationCenter委托
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
Run Code Online (Sandbox Code Playgroud)
步骤3:即使您的应用程序位于前台,此步骤也将允许您的应用程序显示“推送通知”
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler([.alert, .sound])
}
Run Code Online (Sandbox Code Playgroud)
步骤4:此步骤是可选的。检查您的应用程序是否在前台,以及是否在前台,然后显示Local PushNotification。
func application(_ application: UIApplication,didReceiveRemoteNotification userInfo: [AnyHashable: Any],fetchCompletionHandler completionHandler:@escaping (UIBackgroundFetchResult) -> Void) {
let state : UIApplicationState = application.applicationState
if (state == .inactive || state == .background) {
// go to screen relevant to Notification content
print("background")
} else {
// App is in UIApplicationStateActive (running in foreground)
print("foreground")
showLocalNotification()
}
}
Run Code Online (Sandbox Code Playgroud)
本地通知功能-
fileprivate func showLocalNotification() {
//creating the notification content
let content = UNMutableNotificationContent()
//adding title, subtitle, body and badge
content.title = "App Update"
//content.subtitle = "local notification"
content.body = "New version of app update is available."
//content.badge = 1
content.sound = UNNotificationSound.default()
//getting the notification trigger
//it will be called after 5 seconds
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 1, repeats: false)
//getting the notification request
let request = UNNotificationRequest(identifier: "SimplifiedIOSNotification", content: content, trigger: trigger)
//adding the notification to notification center
notificationCenter.add(request, withCompletionHandler: nil)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
189988 次 |
| 最近记录: |