3 回答
TA贡献2021条经验 获得超8个赞
尝试使用这个:
@TargetApi(Build.VERSION_CODES.O)
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void show_Notification(){
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
String CHANNEL_ID="MYCHANNEL";
NotificationChannel notificationChannel=new NotificationChannel(CHANNEL_ID,"name",NotificationManager.IMPORTANCE_LOW);
PendingIntent pendingIntent=PendingIntent.getActivity(getApplicationContext(),1,intent,0);
Notification notification=new Notification.Builder(getApplicationContext(),CHANNEL_ID)
.setContentText("Heading")
.setContentTitle("subheading")
.setContentIntent(pendingIntent)
.addAction(android.R.drawable.sym_action_chat,"Title",pendingIntent)
.setChannelId(CHANNEL_ID)
.setSmallIcon(android.R.drawable.sym_action_chat)
.build();
NotificationManager notificationManager=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(notificationChannel);
notificationManager.notify(1,notification);
}
TA贡献1995条经验 获得超2个赞
要从 php 服务器发送Firebase 推送通知,您可以使用此代码。
<?php
function sendMessage($data, $target, $serverKey){
//FCM api URL
$rsp = [];
$url = 'https://fcm.googleapis.com/fcm/send';
//api_key available in Firebase Console -> Project Settings -> CLOUD MESSAGING -> Server key
$server_key = $serverKey;
$fields = array();
$fields['data'] = $data;
if(is_array($target)){
$fields['registration_ids'] = $target;
}else{
$fields['to'] = $target;
}
//header with content_type api key
$headers = array(
'Content-Type:application/json',
'Authorization:key='.$server_key
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($fields));
$result = curl_exec($ch);
if ($result === FALSE) {
//die('FCM Send Error: ' . curl_error($ch));
}
curl_close($ch);
//print_r($result);
return $result;
}
如果你想更新你的 android 代码,你可以关注这篇文章:Firebase 推送通知
TA贡献1779条经验 获得超6个赞
你得到错误的数据。它应该是这样的。或尝试先打印数据然后从数组中获取。
sendNotification(remoteMessage.getData().get("message")); Log.d("Push data", remoteMessage.getData().toString());
- 3 回答
- 0 关注
- 78 浏览
添加回答
举报