我想知道我们可以在Xcode中使用Objective-C代码来触发此函数中的推送通知:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// Print message ID.
NSLog(@"Message ID: %@", userInfo[@"gcm.message_id"]);
// Pring full message.
NSLog(@"%@", userInfo);
NSLog(@"Type: %@", userInfo[@"type"]);
}
注意:我已经尝试从Firebase控制台触发推送通知,它成功了,我得到了推送通知,
,但现在我试图从我的后端服务器(Rails)发送推送通知,使用https://github.com/spacialdb/fcm
当我从我的后端服务器发送通知时,我得到了数据,但我很困惑如何处理来自后端服务器的数据以触发iOS中的推送通知,我可以使用什么方法?
编辑:我可以在didReceiveRemoteNotification中使用此代码获得通知
UILocalNotification* n1 = [[UILocalNotification alloc] init];
n1.alertBody = [NSString stringWithFormat:@"John commented on your post"];
n1.soundName = UILocalNotificationDefaultSoundName;
n1.fireDate = [NSDate date];
[[UIApplication sharedApplication] presentLocalNotificationNow:n1];
但现在的问题是,通知只出现当我打开我的应用程序,我可以让通知出现当我的iPhone在后台或锁定?
我有办法了!只需在我的rails
中添加这个选项参数 options[:notification] = {}
options[:notification][:title] = 'title'
options[:notification][:body] = 'Calvin Sugianto vote on your post'
options[:content_available] = true
options[:notification][:sound] = "default"
前面的代码
response = fcm.send(registration_ids, options)