我正在使用Swift 3进行编码,而我只是尝试发送现在的通知而没有任何延迟或间隔。但是通知永远不会被触发。这是我的代码..
ViewController代码
import UserNotifications
class HomeViewController: UIViewController{
var isGrantedNotificationAccess:Bool = false
override func viewDidLoad() {
super.viewDidLoad()
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert,.sound,.badge],
completionHandler: { (granted,error) in
self.isGrantedNotificationAccess = granted
})
if isGrantedNotificationAccess{
triggerNotification()
}
}
//triggerNotification func goes here
}
触发通知功能:
func triggerNotification(){
let content = UNMutableNotificationContent()
content.title = NSString.localizedUserNotificationString(forKey: "Notification Testing", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: "This is a test", arguments: nil)
content.sound = UNNotificationSound.default()
content.badge = (UIApplication.shared.applicationIconBadgeNumber + 1) as NSNumber;
let trigger = UNTimeIntervalNotificationTrigger(
timeInterval: 1.0,
repeats: false)
let request = UNNotificationRequest.init(identifier: "testTriggerNotif", content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request)
}
我做错了什么?
答案 0 :(得分:7)
您错过了处理,当应用处于前台时,您没有指定通知的外观或显示方式。
在添加通知时设置以下行,以指定您在用户使用应用时显示横幅广告(iOS 10新功能)。
在构建 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/bgcolor"
android:orientation="horizontal"
android:padding="10dip" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="25dip"
android:gravity="center"
android:textSize="24.5sp"
android:visibility="gone" />
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:stretchColumns="*" >
<TableRow
android:id="@+id/locationsRowss"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:gravity="center_horizontal"
android:minHeight="40dp"
android:weightSum="2" >
<TextView
android:id="@+id/tvlocation11"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginBottom="0.5dp"
android:layout_weight="1"
android:background="@drawable/cellshape"
android:gravity="left|center"
android:paddingLeft="10dp"
android:text="Journey Date"
android:textColor="@color/textcolor"
android:textSize="15dp"
android:textStyle="bold" />
<Button
android:id="@+id/tvJourneyDate"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginBottom="0.5dp"
android:layout_marginLeft="0.5dp"
android:layout_weight="1"
android:background="@drawable/cellshape"
android:gravity="left|center|center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="2/3/2017"
android:textColor="@color/vehicledetailcolor"
android:textSize="15dp" />
</TableRow>
</LinearLayout>
对象时添加以下代码行:
UNMutableNotificationContent
您还应该在AppDelegate中添加以下方法:
content.setValue("YES", forKeyPath: "shouldAlwaysAlertWhileAppIsForeground")
答案 1 :(得分:0)
UNUserNotificationCenter.current()。requestAuthorization&#00;完成处理程序异步处理(该方法在不同的线程中处理)。这就是为什么&#34;如果isGrantedNotificationAccess ..&#34;在completionHandler完成之前读取了代码中的语句。
有一些方法可以解决这个问题。一种是使用NotificationCenter将requestAuthorization方法的结束通知给HomeViewController(参见下面的示例)。
import UserNotifications
class HomeViewController: UIViewController{
let notification = Notification.Name("requestAuthorizationComplete")
override func viewDidLoad() {
super.viewDidLoad()
// Register self as Observer to receive notification
NotificationCenter.default.addObserver(self, selector: #selector(self.triggerNotification), name: notification, object: nil)
UNUserNotificationCenter.current().requestAuthorization(
options: [.alert,.sound,.badge],
completionHandler: { (granted,error) in
NotificationCenter.default.post(name: self.notification, object: nil)
})
}
func triggerNotification(notification:NSNotification?){
// this function will be called once requestAuthorization is complete
}