ios - 当应用程序处于前台时(颤振)在 iOS 中接收 fcm
问题描述
当应用程序打开时,我无法在 iOS 中显示消息。安卓运行良好。我正在接收消息,但格式类似于 = ['aps']['alert'],我可以以某种方式对其进行转换,以便能够使用本地通知包来显示它,就像我在 android 中显示的那样前景。
这就是我显示通知的方式(在 onMessage 中):
_notifications.showNotification(message['notification']['body']);
这是 fcm 的配置方式:
_fcm.requestNotificationPermissions(
const IosNotificationSettings(sound: true, badge: true, alert: true));
_fcm.onIosSettingsRegistered.listen((IosNotificationSettings settings) {
});
_fcm.getToken().then((value) {
});
当我从 firebase 控制台发送测试消息时会出现问题。另外,如果我这样发送它们:
public static function sendNotificationToRegisteredUsers($message){
$msg = array
(
'body' => $message,
'title' => 'Test',
'vibrate' => 1,
'sound' => 1,
'vibrate' => 1,
'sound' => 1,
'largeIcon' => 'large_icon',
'smallIcon' => 'small_icon'
);*/
);
$fields = array
(
'to' => '/topics/TestTopic',
'notification' => $msg,
'priority' => 'high',
'android' => [
//'collapseKey' => 'daily_event',
'priority' => 'high',
/*'notification' => [
'sound' => 'default'
]*/
],
'apns' => [
'headers' => [
'apns-priority' => '10'
],
'payload' => [
'aps' => [
'alert' => [
'title' => 'Test',
'body' => $message,
],
'badge' => 42,
],
],
]
);
$headers = array
(
'Authorization: key=' . API_ACCESS_KEY,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
curl_setopt( $ch,CURLOPT_POST, true );
curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
$result = curl_exec($ch );
curl_close( $ch );
echo $result;
}```
Thank you
解决方案
Add the following lines to the didFinishLaunchingWithOptions method in the AppDelegate.m/AppDelegate.swift file of your iOS project
Objective-C:
if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>) self;
}
Swift:
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
}
推荐阅读
- c++ - 有没有办法从cpp中的文本生成条形码图像?
- node.js - Lambda 正在发送先前的事件而不是“实时”
- python - 二叉树的总和没有按预期的递归运行
- mysql - MySQL,从同一个表中选择 2+ 个匹配项
- angular - 是否可以将组件的内容(ng-content)作为字符串获取?
- cypress - 如何检查元素宽度是否小于或等于赛普拉斯中的像素大小?
- c++ - 我可以使用 malloc 为类对象分配内存吗?
- jenkins - Jenkins插件gitChangelog:如何指定存储库路径
- sql - SQL:为每个 id 增加列值(循环?)
- python - 如何使用线程防止程序“冻结”