我的应用有推送通知,我可以在提示中显示推送通知消息吗?
注意:当用户点击通知时,它将重定向到应用程序页面并且通知消失,因此此处显示警报中的总通知消息?在iOS应用程序中是否可以?
答案 0 :(得分:0)
(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSString * stringNotify = [NSString stringWithFormat:@"%@",[[userInfo valueForKey:@" aps"] valueForKey:@" alert" ] valueForKey:@" body"]];
NSLog(@"字典是%@",userInfo);
UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@" Notification" message:stringNotify委托:self cancelButtonTitle:@"确定" otherButtonTitles:nil,nil]; [alertView show];
}
答案 1 :(得分:0)
我创建了一个自定义通知,可以帮助您在应用处于前台时显示通知。 请查看以下链接iOSForegroundNotification,然后按照以下步骤操作:
bundle exec guard
和SNotificationView.h
复制到项目中的文件中。SNotificationView.m
添加到#import "SNotificationView.h"
文件中。您已将appicon图像替换/添加到“image.png”。
您已在AppDelegate文件中添加以下行:
Bridging-Header.h
为所有ViewController
添加以下功能let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
if let aps = userInfo["aps"] as? NSDictionary {
if let alert = aps["alert"] as? NSDictionary {
if let message = alert["message"] as? NSString {
prefs.setObject(message, forKey: "notification")
prefs.synchronize()
print("Message: \( message)\n")
}
} else if let alert = aps["alert"] as? NSString {
// Push notification message is added in NSUserDefault
prefs.setObject(alert, forKey: "notification")
prefs.synchronize()
if (application.applicationState == UIApplicationState.Active ) {
if settings?.types & UIUserNotificationType.Alert != nil {
// .Alert is one of the valid options
// If you want to add the badge add the line below
UIApplication.sharedApplication().applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
//Call the custom Notification
NSNotificationCenter.defaultCenter().postNotificationName("remotenotification", object: nil)
}
else
{
// This part will be called if you app notification is set to "NONE"
print("No alert ")
}
}
}
在func callNotification()
{
let prefs = NSUserDefaults.standardUserDefaults()
let alert = prefs.stringForKey("notification")!
SNotificationView.showNotificationViewWithImage(UIImage(named:"image.png"), title: "XcodeProject", message: alert, isAutoHide: true, onTouch: {
SNotificationView.hideNotificationViewOnComplete(nil)
})
}
viewDidLoad
希望这可能会有所帮助