FireBase Cloud Messaging Not Working

6 浏览
0 Comments

FireBase Cloud Messaging Not Working

我正在尝试使用Firebase Cloud Messaging。我能够接收到令牌,但无法从控制台接收任何通知。这是我的清单文件:

 








    
        
            
            
        
    
    
        
            
        
    
    
        
            
        
    

我的代码与示例几乎相同:

public class NotificationService extends FirebaseMessagingService {
private void sendNotification(String messagebody) {
    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);
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.icon)
            .setContentTitle("Notified")
            .setContentText(messagebody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0 , notificationBuilder.build());
}
private void sendSnackbar(String messagebody)
{
    Toast.makeText(this,"Notified",Toast.LENGTH_LONG).show();
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    Toast.makeText(this,"Message Received",Toast.LENGTH_LONG).show();
        Log.d("Notification Received",remoteMessage.getNotification().getBody());
        sendNotification(remoteMessage.getNotification().getBody());
    sendSnackbar(remoteMessage.getNotification().getBody());
}
}

我检查了这里的问题Firebase cloud messaging notification not received by device,但仍然不起作用。

0
0 Comments

原因:在代码中没有正确配置Firebase Cloud Messaging的服务。

解决方法:将以下代码添加到应用程序的AndroidManifest.xml文件中,确保将"your.package.name"替换为正确的包名。

<service

android:name="your.package.name.TokenService">

<intent-filter>

<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>

</intent-filter>

</service>

<service

android:name="your.package.name.NotificationService">

<intent-filter>

<action android:name="com.google.firebase.MESSAGING_EVENT"/>

</intent-filter>

</service>

如果还有问题,请告知。

0
0 Comments

问题出现的原因:可能是由于某些原因导致Firebase Cloud Messaging(FCM)无法正常工作。

解决方法:尝试使用服务API调用发送消息。它将提供响应代码和响应JSON,这可能有助于您找到FCM不起作用的原因。

以下是服务调用的用法:Firebase onMessageReceived not called when app in background

0