gpt4 book ai didi

android - Flutter firebase消息未收到通知

转载 作者:行者123 更新时间:2023-12-04 11:27:57 25 4
gpt4 key购买 nike

我目前正在使用 firebase 消息处理 flutter 通知,我无法收到任何通知。我使用 curl 通过服务器发送了通知,我可以让它在原生 android 应用程序(Android Studio)中工作,但不能在 flutter 中工作,任何帮助将不胜感激。下面是我的代码。
Flutter 通知代码

class FirebaseNotifications {
FirebaseMessaging _firebaseMessaging;

void setUpFirebase() {
_firebaseMessaging = FirebaseMessaging();
firebaseCloudMessaging_Listeners();
}

void firebaseCloudMessaging_Listeners() {
if (Platform.isIOS) iOS_Permission();

_firebaseMessaging.getToken().then((token) {
print(token);
});

_firebaseMessaging.configure(
onMessage: (Map<String, dynamic> message) async {
print('on message $message');
},
onResume: (Map<String, dynamic> message) async {
print('on resume $message');
},
onLaunch: (Map<String, dynamic> message) async {
print('on launch $message');
},
);
}

void iOS_Permission() {
_firebaseMessaging.requestNotificationPermissions(
IosNotificationSettings(sound: true, badge: true, alert: true));
_firebaseMessaging.onIosSettingsRegistered
.listen((IosNotificationSettings settings) {
print("Settings registered: $settings");
});
}
}
Android Studio 代码
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

Map<String, String> params = remoteMessage.getData();

JSONObject object = new JSONObject(params);

showNotification(remoteMessage);
}

private void showNotification(RemoteMessage remoteMessage) {
Map data = remoteMessage.getData();

String mesg = "New Notification";

Intent intent;
PendingIntent pendingIntent;
NotificationCompat.Builder builder;

Intent i = new Intent(this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.GREEN);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{0, 250, 250, 250});
mNotificationManager.createNotificationChannel(mChannel);

builder = new NotificationCompat.Builder(this, id);

intent = new Intent(this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

builder.setContentTitle(data.get("title").toString()) // required
//.setStyle(new
NotificationCompat.BigTextStyle().bigText(remoteMessage.getNotification()
.getBod
y().toString()))
.setStyle(new
NotificationCompat.BigTextStyle().bigText(mesg)) //custom data
.setSmallIcon(R.drawable.ic_stat_notification_icon4) //
required

.setContentText(mesg) // custom data
.setWhen(System.currentTimeMillis())
.setDefaults(Notification.DEFAULT_ALL)

.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setVibrate(new long[]{0, 250, 250, 250});
}
}
服务器端代码
function sendtoUser($sendingTarget, $acc_name, $type_message, 
$type_of_transaction, $check_amount)
{

#API access key from Google API's Console
$API_ACCESS_KEY = "adazxc";
$registrationIds = $sendingTarget;
#prep the bundle

$fields = array(
'to' => $registrationIds,
'data' => array(
'title' => 'my title',
'keyValue' => true,
'receiverName' => $acc_name,
'transType' => $type_of_transaction,
'totalAmount' => $check_amount
),
);

$headers = array(
'Authorization: key=' . $API_ACCESS_KEY,
'Content-Type: application/json'
);
#Send Reponse To FireBase Server
$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);
}
编辑
我以某种方式可以收到通知,但我注意到我的状态栏没有收到通知,但它打印在控制台中,但是,当我关闭应用程序时,我可以收到通知。那么我如何在这两种情况下都能收到通知呢?

最佳答案

data标签仅用于将更多信息传输到应用程序,例如必须重定向的位置、图像等。您必须添加 notification标记以在状态栏上显示通知。

看来你还没有添加通知内容,请引用下面的示例消息并配置相同,它会工作

{
"notification": {
"body": "body",
"title": "title"
},
"priority": "high",
"data": {
"click_action": "FLUTTER_NOTIFICATION_CLICK",
"id": "1",
"status": "done",
"image": "https://ibin.co/2t1lLdpfS06F.png",
},
"to": <fcm_token>
}

关于android - Flutter firebase消息未收到通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56253721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com