【发布时间】:2015-09-23 12:55:42
【问题描述】:
我的应用有推送通知,我可以在警报中显示推送通知消息吗?
注意:当用户单击通知时,它将重定向到应用程序页面并且通知消失,所以这里在警报中显示总通知消息?在iOS应用程序中可以吗?
【问题讨论】:
-
我部分理解你的问题,有可能
标签: ios xcode6 apple-push-notifications uialertview
我的应用有推送通知,我可以在警报中显示推送通知消息吗?
注意:当用户单击通知时,它将重定向到应用程序页面并且通知消失,所以这里在警报中显示总通知消息?在iOS应用程序中可以吗?
【问题讨论】:
标签: ios xcode6 apple-push-notifications uialertview
(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" 消息:stringNotify 委托:self 取消按钮标题:@“确定” 其他按钮标题:无,无]; [alertView 显示];
}
【讨论】:
我创建了一个自定义通知,它将帮助您在应用处于前台时显示通知。 检查以下链接iOSForegroundNotification 并按照以下步骤操作:
SNotificationView.h 和SNotificationView.m 复制到项目的文件中。#import "SNotificationView.h" 添加到您的 Bridging-Header.h 文件中。您已将 appicon 图像替换/添加到“image.png”。
您已在 AppDelegate 文件中添加以下行:
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 ")
}
}
}
为所有的 ViewController 添加以下函数
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 中添加以下行
NSNotificationCenter.defaultCenter().addObserver(self, selector: "callNotification", name: "remotenotification", object: nil)
希望这可能会有所帮助
【讨论】: