【问题标题】:Unable to receive push notification from fcm in android无法在 android 中接收来自 fcm 的推送通知
【发布时间】:2017-04-25 11:04:30
【问题描述】:

我正在尝试从 FCM 发送推送通知。但无法在我的 android 设备中接收。 起初您可以在图像中看到它没有被初始化,但后来初始化成功。 我可以获取 FCM 注册 ID,但无法获取推送消息。

这里是截图:

OnReceive 方法:

   mRegistrationBroadcastReceiver = new BroadcastReceiver() {
             @Override
            public void onReceive(Context context, Intent intent) {

            // checking for type intent filter
            if (intent.getAction().equals(Config.REGISTRATION_COMPLETE)) {
                // gcm successfully registered
                // now subscribe to `global` topic to receive app wide notifications
                FirebaseMessaging.getInstance().subscribeToTopic(Config.TOPIC_GLOBAL);

                displayFirebaseRegId();

            } else if (intent.getAction().equals(Config.PUSH_NOTIFICATION)) {
                // new push notification is received

                String message = intent.getStringExtra("message");

                Toast.makeText(getApplicationContext(), "Push notification: " + message, Toast.LENGTH_LONG).show();

                txtMessage.setText(message);
            }
        }
    };

我正在使用AndroidHive 的这个示例,并且我也尝试在不同版本的不同设备上对其进行测试。

如果有人能提供一些想法,那将对我非常有帮助。

【问题讨论】:

  • 你如何发送推送通知?
  • 来自谷歌的 fcm
  • 我的意思是使用您自己的服务器或 fcm 控制台?
  • 我是从 FCM 的控制台发送的
  • 不需要使用 Brodcast Receiver 只需使用 public class MyFcm extends FirebaseMessagingService

标签: android firebase push-notification firebase-cloud-messaging


【解决方案1】:

由于我们需要连接到网络,所以在 AndroidManifest.xml 文件中添加 Internet 权限。还要添加振动,因为我们将在 android 设备上生成通知警报。 AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.VIBRATE"/>

还为扩展 FirebaseMessagingService 的服务添加条目,以便在后台收到有关应用程序的通知后处理消息。我们将扩展这项服务。在应用程序中接收通知。这对于在应用不活动时运行 android 推送通知是绝对必要的。

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

为扩展 FirebaseInstanceIdService 的服务添加一个条目,该服务将用于处理注册令牌生命周期。这是向特定设备/设备组发送消息所必需的。

<service
    android:name=".MyAndroidFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

添加功能 创建一个新的 java 类 MyAndroidFirebaseMsgService 并添加以下代码。 它是一个扩展 FirebaseMessagingService 的服务。它在后台执行所有类型的消息处理,并在可用时发送推送通知。

public class MyAndroidFirebaseMsgService extends FirebaseMessagingService {
    private static final String TAG = "MyAndroidFCMService";
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.getFrom());
        Log.d(TAG, "Notification Message Body: " + 
        remoteMessage.getNotification().getBody());
        //create notification
        createNotification(remoteMessage.getNotification().getBody());
    }

    private void createNotification( String messageBody) {
        Intent intent = new Intent( this , ResultActivity. class );
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent,
        PendingIntent.FLAG_ONE_SHOT);

        Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("Bingo")
                        .setContentText(messageBody)
                        .setAutoCancel( true )
                        .setSound(notificationSoundURI)
                        .setContentIntent(resultIntent);

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

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

在收到消息时调用 onMessageReceived()。在此函数中,我们将消息记录到 LogCat 控制台并使用消息文本调用 createNotification()。 createNotification() 方法会在 android 通知区创建推送通知。

【讨论】: