【问题标题】:Not recieving the notification from firebase未收到来自 firebase 的通知
【发布时间】:2017-09-22 07:18:42
【问题描述】:

我有一个集成了 firebase 的 android 应用。

  • 我一开始就成功收到了 Firebase 注册 ID。

但是当我将消息从 firebase 控制台发送到我的应用程序时,我没有在手机中收到它

这是我的应用程序 gradle

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.google.firebase:firebase-auth:11.0.4'
compile 'com.android.support:design:25.3.1'
compile 'com.google.firebase:firebase-core:10.2.1'
compile 'com.google.firebase:firebase-messaging:10.2.1'

构建等级

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

我有一个单独的类 MyFirebaseMessagingService,它扩展了 FirebaseMessagingService

 public void onMessageReceived(RemoteMessage remoteMessage) {
    Log.e(TAG, "From: " + remoteMessage.getFrom());
    Log.e("tagg", "From: " + remoteMessage.getFrom());

    if (remoteMessage == null)
        return;

    // Check if message contains a notification payload.
    if (remoteMessage.getNotification() != null) {
        Log.e(TAG, "Notification Body: " + remoteMessage.getNotification().getBody());
        handleNotification(remoteMessage.getNotification().getBody());
    }

    // Check if message contains a data payload.
    if (remoteMessage.getData().size() > 0) {
        Log.e(TAG, "Data Payload: " + remoteMessage.getData().toString());

        try {
            JSONObject json = new JSONObject(remoteMessage.getData().toString());
            handleDataMessage(json);
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.getMessage());
        }
    }

我在这个类中没有得到任何日志。

怎么可能发生错误,我已经成功收到了 reg id ,那么可能是什么错误

【问题讨论】:

  • 您是否尝试使用来自 firebase 的令牌 (onTokenRefresh) 发送消息?
  • 当您尝试发送通知时,您的应用是否处于前台?尝试在应用处于后台时发送通知
  • 请点击此链接firebase.google.com/docs/cloud-messaging/android/client 将firebase消息完整集成到android中。
  • 看我的回答可能是因为错过了。

标签: android firebase firebase-cloud-messaging firebase-notifications


【解决方案1】:

您是否已将该服务添加到您的 AndroidManifest 中?

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

【讨论】:

    【解决方案2】:

    将服务添加到您的 AndroidManifest

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

    【讨论】:

      【解决方案3】:
      private void HandleNotification(String messagebody) {
          Intent intent = new Intent(this, your.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);
          android.support.v4.app.NotificationCompat.Builder notificationBuilder = new android.support.v4.app.NotificationCompat.Builder(this)
                  .setSmallIcon(R.mipmap.logo)
                  .setContentTitle("")
                  .setContentText(messageBody)
                  .setAutoCancel(true)
                  .setSound(defaultSoundUri)
                  .setContentIntent(pendingIntent);
      
          NotificationManager notificationManager =
                  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      
          notificationManager.notify(0, notificationBuilder.build());
      }
      

      【讨论】:

      • 并将其称为 handleNotification(remoteMessage.getNotification().getBody());
      猜你喜欢
      • 2018-06-21
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      • 1970-01-01
      • 2019-04-19
      • 2019-01-30
      • 1970-01-01
      相关资源
      最近更新 更多