5

dự án của tôi sử dụng Firebase Notifications làm dịch vụ APN, nhưng tôi đã sử dụng Firebase Console để gửi thông báo đến thiết bị của tôi như một thử nghiệm. Khi ứng dụng ở chế độ nền hoặc thiết bị ở màn hình khóa, không có thông báo nào đến thiết bị. Tuy nhiên, đầu ra của giao diện điều khiển cuối cùng cũng đến từ phương thức applicationReceivedRemoteMessage khi tôi mở lại ứng dụng.applicationReceivedRemoteMessage chỉ thực hiện ở tiền cảnh

func applicationReceivedRemoteMessage(_ remoteMessage: FIRMessagingRemoteMessage) { 

     print("%@", remoteMessage.appData) 
     print("QQQQQ") 
    } 

Kết quả ví dụ:

%@ [AnyHashable("notification"): { 
    body = Hi; 
    e = 1; 
}, AnyHashable("from"): 492525072004, AnyHashable("collapse_key"): org.myApp] 
QQQQQ 
+0

Bản sao có thể có của [Không nhận được APN khi không tham gia ứng dụng] (http://stackoverflow.com/questions/40295475/not-receiving-apns-when-out-of-app) – Chris

+0

Câu hỏi mà bạn đã giới thiệu tôi đến không có câu trả lời và được diễn đạt khác nhau. –

Trả lời

0

Có một vấn đề với iOS và thông điệp dữ liệu. Nó khẳng định rằng here

Trên iOS, FCM lưu trữ thông điệp và cung cấp nó chỉ khi ứng dụng trong foreground và đã thiết lập một kết nối FCM.

Vì vậy, PHẢI có công việc xung quanh. Một cái gì đó tương tự như tôi:

Gửi 2 push thông báo:

1) Bình thường để đánh thức những người sử dụng điện thoại/khởi trong khi ứng dụng là trong nền sử dụng mã này:

{ 
    "to" : "/topics/yourTopicName", 
    "notification" : { 
    "priority" : "Normal", 
    "body" : "Notification Body like: Hey! There something new in the app!", 
    "title" : "Your App Title (for example)", 
    "sound" : "Default", 
    "icon" : "thisIsOptional" 
    } 
} 

2) Thông báo dữ liệu sẽ kích hoạt khi người dùng mở ứng dụng

{ 
    "to" : "/topics/yourTopicName", 
    "data" : { 
     "yourData" : "1", 
     "someMoreOfYourData" : "This is somehow the only workaround I've come up with." 
    } 
} 

và, vì vậy, theo phương pháp - (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage xử lý dữ liệu của bạn:

- (void)applicationReceivedRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage { 
// Print full message 
NSLog(@"%@", remoteMessage.appData); 
// 
//*** ABOUT remoteMessage.appData ***// 
// remoteMessage.appData is a Key:Value dictionary 
// (data you sent with second/data notification) 
// so it's up to you what will it be and how will the 
// app respond when it comes to foreground. 
} 

tôi cũng sẽ để lại đoạn mã này để kích hoạt các thông báo bên trong ứng dụng (tạo thông báo địa phương), vì bạn có thể sử dụng nó để tạo ra một biểu ngữ im lặng, có thể, do đó, người dùng được thông báo lại ngay cả khi ứng dụng đến tiền cảnh:

NSDictionary *userInfo = remoteMessage.appData;  
UILocalNotification *localNotification = [[UILocalNotification alloc] init]; 
localNotification.userInfo = userInfo; 
localNotification.soundName = UILocalNotificationDefaultSoundName; 
localNotification.alertBody = userInfo[@"yourBodyKey"]; 
localNotification.alertTitle = userInfo[@"yourTitleKey"]; 
localNotification.fireDate = [NSDate date]; 
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification]; 

Nó sẽ kích hoạt thông báo tương tự ứng dụng thứ hai xuất hiện ở tiền cảnh.

Các vấn đề liên quan