首页 > 解决方案 > 当应用程序处于前台时(颤振)在 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

标签: iosfirebaseflutterfirebase-cloud-messaging

解决方案


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
}

推荐阅读