Kin*_*ing 1 php android push-notification
我对此完全迷失了,打开了 20 个标签来解决这个烦人的难题。我已经阅读了多个教程,但没有一个回答在哪里定义频道 ID 以及如何通过 PHP 向它发送通知。
override fun onMessageReceived(remoteMessage: RemoteMessage?) {
showNotification(remoteMessage!!.data["message"]!!)
}
private fun showNotification(message: String) {
val i = Intent(this, MainActivity::class.java)
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT)
val builder = NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle("FCM Test")
.setContentText(message)
.setLargeIcon(BitmapFactory.decodeResource(resources, com.amoflirt.amoflirt.R.drawable.ic_vip_heart))
.setSmallIcon(com.amoflirt.amoflirt.R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(pendingIntent)
val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.notify(0, builder.build())
}
Run Code Online (Sandbox Code Playgroud)
无论我在这里更改什么,它都不会影响发送/接收通知的方式,就像 setLargeIcon 没有添加任何内容一样。
这是PHP:
define( 'API_ACCESS_KEY', 'xxx');
$msg = array(
'body' => $body,
'title' => $title,
);
$fields = array(
'to' => $token,
'notification' => $msg
);
$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 );
echo $result;
curl_close( $ch );
Run Code Online (Sandbox Code Playgroud)
我尝试了各种“解决方案”来在两侧(android 和 PHP)添加一个大图标和振动,但它们都不起作用。但最重要的是我不明白如何添加该死的频道以及如何向他们发送通知。为什么android作为一个整体必须如此混乱和一堆狗屎:(
提前致谢
我更新了@Nasdomlan 的答案,但没有人经过同行评审并接受它。所以我在这里发布我自己的答案:
$msg = array(
'body' => $body,
'title' => $title,
'android_channel_id' => $id
);
$fields = array(
'to' => $token,
'notification' => $msg
);
Run Code Online (Sandbox Code Playgroud)
其中 $id 您的 channel_id 字符串。您可以在https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support上阅读有关它的信息,“表 2b. Android — 通知消息的键”
归档时间: |
|
查看次数: |
2395 次 |
最近记录: |