【问题标题】:Unable to generate notification from onMessageReceived of FirebaseMessagingService in Android无法从 Android 中 FirebaseMessagingService 的 onMessageReceived 生成通知
【发布时间】:2020-02-11 09:35:10
【问题描述】:

我正在发送仅包含来自 Firebase 云消息传递 HTTP 协议的数据负载的通知。我可以在我的 Android 应用程序中的“FirebaseMessagingService”的“onMessageReceived”回调方法中看到通知正确到达。问题是我无法从这里生成通知。我已经编写了用于生成通知的代码,但通知没有出现在手机的通知区域中。

下面是我的代码:

public class MyFirebaseMessagingService extends FirebaseMessagingService {

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

    String data = remoteMessage.getData().toString();
    Log.d("<<<>>>", "MyFirebaseMessagingService > onMessageReceived > data : " + data);

    String title = remoteMessage.getData().get("title");
    String body = remoteMessage.getData().get("body");

    showNotification(title, body);
}

private void showNotification(String title, String body) {

    Log.d("<<<>>>", "MyFirebaseMessagingService > showNotification() called !");

    Log.d("<<<>>>", "title : " + title);
    Log.d("<<<>>>", "body : " + body);

    Intent intent = new Intent(this, SplashScreenActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, "NewsVault_Notification_Channel")
            .setSmallIcon(R.drawable.firebase_notification_icon)
            .setContentTitle(title)
            .setContentText(body)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);

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

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

}

下面是我的 Logcat 输出:

D/<<<>>>: MyFirebaseMessagingService > onMessageReceived > data : 
{channel_id=NewsVault_Notification_Channel, priority=high, body=Test body, key_1=Value for key_1,     
key_2=Value for key_2, title=Test title, content_available=true}
D/<<<>>>: MyFirebaseMessagingService > showNotification() called !
D/<<<>>>: title : Test title
D/<<<>>>: body : Test body

我在 Manifest 文件中添加了以下内容:

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

<meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="NewsVault_Notification_Channel" />

【问题讨论】:

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


    【解决方案1】:
            if (isAppIsInBackground(context)) {
            int res = Utility.getRandomId();
            PendingIntent pendingIntent;
    
            Intent intent = setActivityIntent(notifyDataModel);
            intent.putExtra(AppConstants.BOOKING_ID, id);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            pendingIntent = PendingIntent.getActivity(context, res, intent, 0);
    
            mBuilder.setContentTitle(notifyDataModel.getStrTitle())
                    .setContentText(notifyDataModel.getStrMessage())
                    .setNumber(1)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(true)
                    .setColor(ContextCompat.getColor(context, R.color.colorAccent))
                    .setContentIntent(pendingIntent);
            getManager().notify(res, mBuilder.build());
    
        } else {
            int res = Utility.getRandomId();
            PendingIntent pendingIntent;
            Intent intent = setActivityIntent(notifyDataModel);
            intent.putExtra(AppConstants.BOOKING_ID, id);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            pendingIntent = PendingIntent.getActivity(context, res, intent, 0);
    
            mBuilder.setContentTitle(notifyDataModel.getStrTitle())
                    .setContentText(notifyDataModel.getStrMessage())
                    .setNumber(1)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher))
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setAutoCancel(true)
                    .setColor(ContextCompat.getColor(context, R.color.colorAccent))
                    .setContentIntent(pendingIntent);
            getManager().notify(res, mBuilder.build());
    
        }
    

    【讨论】:

      【解决方案2】:

      对于 Android 版本 >= Oreo(API 级别 26),您必须创建通知通道,如下所示:

      if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                  NotificationChannel channel = new NotificationChannel("General", "General notification", NotificationManager.IMPORTANCE_DEFAULT);
                  channel.setVibrationPattern(new long[]{1000, 1000, 1000, 1000, 1000});
      
                  AudioAttributes attributes = new AudioAttributes.Builder()
                          .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                          .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
                          .build();
      
                  channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI, attributes);
                  mNotificationManager.createNotificationChannel(channel);
              }
      

      【讨论】:

        【解决方案3】:

        您是否在 AndroidManifest.xml 中添加了? :

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

        【讨论】:

        • 是的,我已经添加了。我还定义了 default_notification_channel_id。
        • 尝试将 NotificationChannel 的重要性设置得更高(channel.setImportance(NotificationManager.IMPORTANCE_DEFAULT);),或者你的 notificationBuilder 优先级更高! (.setPriority(PRIORITY_DEFAULT))
        【解决方案4】:

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

            String channelId ="100";
            Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            NotificationCompat.Builder notificationBuilder =
                    new NotificationCompat.Builder(this, channelId)
                            .setSmallIcon(R.mipmap.ic_launcher)
                            .setContentTitle(title)
                            .setContentText("demo")
                            .setAutoCancel(true)
                            .setSound(defaultSoundUri)
                            .setContentIntent(pendingIntent);
        
            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                NotificationChannel channel = new NotificationChannel(channelId,
                        "test",
                        NotificationManager.IMPORTANCE_DEFAULT);
                channel.enableLights(true);
                notificationManager.createNotificationChannel(channel);
            }
            notificationManager.notify(0, notificationBuilder.build());
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-10-01
          • 2020-02-06
          • 2018-04-19
          • 2020-09-11
          相关资源
          最近更新 更多