douzi1986 2019-01-18 02:12
浏览 83

Android FCM定义频道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())
}

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

This is the 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 );

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 :(

Thanks in advance

  • 写回答

2条回答 默认 最新

  • doushuzd3033 2019-02-26 18:10
    关注
     $msg = array(
                'body'  => $body,
                'title' => $title,
                'channel_id' => $id,
                );
    
    $fields = array(
                    'to' => $token,
                    'notification'  => $msg
                    );
    

    where $id your channel_id string

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)