Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Firebase-can';t设置通知的标题和消息_Android_Firebase_Firebase Cloud Messaging - Fatal编程技术网

Android Firebase-can';t设置通知的标题和消息

Android Firebase-can';t设置通知的标题和消息,android,firebase,firebase-cloud-messaging,Android,Firebase,Firebase Cloud Messaging,我从PHP服务器向Firebase API发送了以下通知数据: $fields = [ 'to' => "fcm....", 'notification' => [ 'title' => 'Test Test Test', 'body' => 'MSG MSG MSG' ], 'data' => [ 'type' => 'message', 'sender' =&g

我从PHP服务器向Firebase API发送了以下通知数据:

$fields = [
    'to' => "fcm....",
    'notification' => [
        'title' => 'Test Test Test',
        'body' => 'MSG MSG MSG'
    ],
    'data' => [
        'type' => 'message',
        'sender' => 'someone'
    ],
    'priority' => 'high'
];
在android studio中,这是扩展
FirebaseMessagingService
类的
MyFirebaseMessagingService
类:

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        sendNotification("Test", "Test");
    }

    private void sendNotification(String title, 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_ONE_SHOT);

        String channelId = getString(R.string.notification_channel_id);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.notificatino)
                        .setContentTitle(title)
                        .setContentText(message)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0, notificationBuilder.build());
    }
}
问题是我保留了从服务器获取标题和消息的通知(title,MSG),而不是将标题和消息作为参数传递给
sendNotification


您是否像这样将
MyFirebaseMessagingService
添加到
AndroidManifest.xml
文件中:

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


另外,要考虑到如果应用程序关闭(关闭或不活动),则不会调用该类,并且您需要从服务器正确发送消息数据。

可能尝试调试它-这可能有助于了解问题所在。在“MyFirebaseMessagingService”类中添加一个断点,看看它是否到达了那里。如果它到达了那里,我写“Log.I”(“通知”,“通知”)”并显示它。