Android FCM定义频道ID,并通过PHP

时间:2019-01-18 02:12:45

标签: 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())
}

无论我在此处进行什么更改,它都不会影响发送/接收通知的方式,例如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 );

我尝试了各种“解决方案”,在两侧(Android和PHP)都添加了大图标并增加了振动,但它们均无效。但是最重​​要的是,我不了解如何添加该死的频道以及如何向他们发送通知。为什么整个android都变得如此混乱和一堆狗屎:(

预先感谢

2 个答案:

答案 0 :(得分:0)

 $msg = array(
            'body'  => $body,
            'title' => $title,
            'channel_id' => $id,
            );

$fields = array(
                'to' => $token,
                'notification'  => $msg
                );

其中$id是您的channel_id字符串

答案 1 :(得分:0)

我更新了@Nasdomlan的答案,但没有人经过同行评审并接受。所以我在这里发布自己的答案:

$msg = array(
            'body'  => $body,
            'title' => $title,
            'android_channel_id' => $id
       );

$fields = array(
                'to' => $token,
                'notification'  => $msg
          );

其中$ id是您的channel_id字符串。您可以在https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support的“表2b。Android-通知消息的键”上了解它