【问题标题】:onMessageReceived never called in notification from server using Firebase in AndroidonMessageReceived 从未在 Android 中使用 Firebase 从服务器调用通知
【发布时间】:2016-10-05 09:41:53
【问题描述】:

我正在使用 Android 中的 Firebase 从服务器发送通知。当我从 Firebase 控制台发送通知时,我的代码有效,但当我从服务器发送消息时,它不起作用。我打开调试,分析出FirebaseMessagingService的onMessageReceived方法从来没有被调用过。

这是我的 FirebaseMessagingService 代码:

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
    private static final String TAG = FirebaseMessagingService.class.getSimpleName();

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        showNotification(remoteMessage.getData().get("message"));
    }

     private void showNotification(String message){

            Intent intent = new Intent(this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentTitle("Firebase Push Notification")
                    .setContentText(message)
                    .setAutoCancel(true)
                    .setSound(defaultSoundUri)
                    .setContentIntent(pendingIntent);
            NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(0, notificationBuilder.build());
        }
    }

这是我的服务器端PHP代码"

public function sendPushNotificationToGCMSever($token, $message){

        //include_once 'config.php';

        $url = 'https://fcm.googleapis.com/fcm/send';

        $fields = array(
                'registration_ids' => $token,
                'notification' =>  $message
        );

        $headers = array(
                'Authorization:key=' . SERVER_KEY,
                'Content-Type:application/json'
        );
        $ch = curl_init();

        $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);

        curl_close($ch);

        return $result;
    }

谁能告诉我我做错了什么?任何帮助将非常感激。谢谢你:)

【问题讨论】:

  • 你的应用是在前台还是后台?

标签: php android firebase firebase-cloud-messaging


【解决方案1】:

guide 表示(要像控制台那样发送通知)您的 $fields 应该是(如果 $message 是字符串)

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

您还可以通过设置'icon' => 'the name of some file in res/drawables' 来选择通知图标,如in this blog post 所述

另请参阅指南中的纯数据消息和通知之间的区别。

如果您尝试仅数据消息和onMessageReceived,那么您可能忘记添加(如guide 中所述)

<service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
AndroidManifest.xml

【讨论】:

猜你喜欢
  • 2018-04-19
  • 2019-04-10
  • 1970-01-01
  • 2018-01-22
  • 2016-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
相关资源
最近更新 更多