我目前正在使用Firebase消息处理Flutter通知,但是我没有收到任何通知。我使用curl
通过服务器发送了通知,但是我可以在本机android应用(Android Studio)中使它正常工作,但无法正常运行,我们将不胜感激。下面是我的代码。
颤动通知代码
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);
}
编辑
我以某种方式可以收到通知,但是我注意到状态栏没有收到通知,但状态栏已打印在控制台中,但是,当我关闭应用程序时,我可以收到通知。那么在两种情况下我如何都能收到通知?
答案 0 :(得分:1)
对于它的价值,我有一个问题,就是今天昨天工作正常时收到onMessage
通知(没有尝试onResume
或onLaunch
)。功能日志和令牌中没有错误都相同。
尝试重新启动设备-如果以前的功能正常工作,这似乎是修复此问题的最一致的方法。
另外,如果刚开始时消息传递似乎很慢,则可能是由于一段时间不使用这些功能时冷启动所致(有关更多信息,请参阅此SO question)。
答案 1 :(得分:0)
您可能会错过针对android(以及ios,如果需要)的手动配置,请参考以下链接
答案 2 :(得分:0)
进行一些检查(版本号可能会更改):
在android / build.gradle中:
在android / app / build.gradle中:
答案 3 :(得分:0)
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>
}