无法从android中的fcm接收推送通知

时间:2017-04-25 11:04:31

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

我正在尝试从FCM发送推送通知。但是无法在我的Android设备中接收它。 首先,您可以在图像中看到它未初始化但稍后初始化成功。 我能够获得FCM注册ID,但无法获得推送消息。

这是截图: Logcat

OnReceive Method:

   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的这个示例,我也尝试在不同版本的不同设备上测试它。

如果有人可以提出一些想法,那么对我来说非常有帮助。

1 个答案:

答案 0 :(得分:1)

由于我们需要连接到网络,因此在AndroidManifest.xml文件中添加Internet权限。还要添加Vibrate,因为我们将在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通知区域中创建推送通知。

相关问题