Android FCM 定义频道 ID 并通过 PHP 向该频道发送通知 [英] Android FCM define channel ID and send notification to this channel through PHP

查看:70
本文介绍了Android FCM 定义频道 ID 并通过 PHP 向该频道发送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全迷失于此,打开了大约 20 个标签来解决这个烦人的难题.我已经阅读了多个教程,但没有一个回答在哪里定义频道 ID 以及如何通过 PHP 向其发送通知.

I'm totally lost on this, got like 20 tabs open to solve this annoying puzzle. I've gone though multiple tutorials but none of them answer where to define a channel ID and how to send a notification through PHP to it.

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 没有添加任何内容一样.

No matter what I change here, it doesn't effect the way the notifications are sent/received, like setLargeIcon isn't adding anything.

这是 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作为一个整体必须如此混乱和一堆狗屎:(

I tried all kind of "solutions" to add a large icon and vibration on both sides (android and PHP) but none of them works. But the most important is I don't understand how to add the damn channels and how to send the notifications to them. Why do android as a whole have to be such a mess and pile of dog crap :(

提前致谢

推荐答案

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

I updated @Nasdomlan's answer but no one peer-reviewed & accepted it. so I post my own answer here:

$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 — 通知消息的键"

where $id your channel_id string. You can read about it on https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support , "Table 2b. Android — keys for notification messages"

这篇关于Android FCM 定义频道 ID 并通过 PHP 向该频道发送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆