没有从FCM收到任何通知

时间:2019-10-29 05:06:04

标签: android firebase-cloud-messaging

我正在使用FCM并按照Firebase网站上的说明进行操作,但在后台运行应用程序时通知不起作用

我已经同步了gradle库并下载了json文件并完成了同步,但是在后台运行应用程序时未收到通知

应用gradle

dependencies {
    ///////
}

apply plugin: 'com.google.gms.google-services'

dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        classpath 'com.google.gms:google-services:4.3.2'

          // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

3 个答案:

答案 0 :(得分:0)

有几个步骤可以在后台运行应用程序时获得通知。

  1. 您需要在 Android FCM 服务中使用 POST 参数。
  2. 您需要从FCM依赖项中编写 onMessageReceived()内部的内容。

https://firebase.google.com/docs/cloud-messaging/android/receive#handling_messages

答案 1 :(得分:0)

因为我只是发送没有消息正文的通知 我尝试了解决方案,我发现我忘记了这个gradle库

实现'com.google.firebase:firebase-messaging:20.0.0'

答案 2 :(得分:0)

您必须添加

  

classpath'com.android.tools.build:gradle:3.5.1'

     

classpath'com.google.gms:google-services:4.3.2'

在项目gradle中,而Google插件在应用gradle文件中。 然后添加以下依赖项:

implementation 'com.google.firebase:firebase-core:17.2.0'
implementation 'com.google.firebase:firebase-messaging:19.0.0'

您需要创建FCM服务才能接收通知。

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    private NotificationManager notificationManager;
    private static final String ADMIN_CHANNEL_ID = "CHANNEL_ID";

    @Override
    public void onNewToken(String s) {
        super.onNewToken( s );
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived( remoteMessage );

        Map<String, String> data = remoteMessage.getData();
        String body = data.get("body");
        String title = data.get("title");
        String image = data.get("image");

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

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            setupChannels( title, body );
        }

        Log.d( "Notification", "onMessageReceived: "  + image);

        Intent notificationIntent = new Intent(this, NotificationActivity.class);
        notificationIntent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP );
        PendingIntent pendingIntent = PendingIntent.getService( this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT );
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder( this, ADMIN_CHANNEL_ID )
                .setSmallIcon( R.drawable.ic_launcher_background )
                .setContentTitle( remoteMessage.getNotification( ).getTitle( ) )
                .setContentText( remoteMessage.getNotification( ).getBody( ) )
               .setContentIntent( pendingIntent );

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

        notificationManager.notify( 0, notificationBuilder.build( ) );
    }

    @RequiresApi(api = Build.VERSION_CODES.O)
    private void setupChannels(String adminChannelName, String adminChannelDescription) {
        NotificationChannel adminChannel;
        adminChannel = new NotificationChannel( ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_LOW );
        adminChannel.setDescription( adminChannelDescription );
        adminChannel.enableLights( true );
        adminChannel.setLightColor( Color.RED );
        adminChannel.enableVibration( true );
        if (notificationManager != null) {
            notificationManager.createNotificationChannel( adminChannel );
        }
    }

}

并将此代码添加到您的清单:

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