首页 > 解决方案 > Firebase 通知,图标和大图像中的小故障

问题描述

我在 android studio 中使用 Firebase 为我的应用程序创建了一个通知系统。该应用程序有一个小缺陷。

当应用程序在屏幕上可见时,它可以正常工作,图标正常显示,大图也显示。请参阅下面的屏幕截图。

在此处输入图像描述

关闭App并发送通知时,出现crash,图标显示为球状,大图不出现。请参阅下面的屏幕截图。

在此处输入图像描述

我将发布我正在使用的代码。如果有人已经经历过并且可以帮助我,我将非常感激。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testedep">

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SplashScreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

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

    <meta-data
        android:name="preloaded_fonts"
        android:resource="@array/preloaded_fonts" />

    <activity android:name=".TesteSharedPreferences" />
    <activity android:name=".TesteProgressBar" />
    <activity android:name=".MainActivity" />
    <activity android:name=".ContatosActivity"></activity>
</application>

我的班级 MiFirebaseInstanceIdService

package com.example.testedep;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;


public class MiFirebaseInstanceIdService extends FirebaseInstanceIdService {

public static final String TAG = "NOTICIAS";

@Override
public void onTokenRefresh() {
    super.onTokenRefresh();

    String token = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Token: " + token);

}
}

我的课堂 MiFirebaseMessagingService

package com.example.testedep;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.squareup.picasso.Picasso;

import java.io.IOException;

public class MiFirebaseMessagingService extends FirebaseMessagingService {

public static final String TAG = "NOTICIAS";

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

    String from = remoteMessage.getFrom();
    Log.d(TAG, "Mensaje recebido de: " + from);

    if (remoteMessage.getNotification() != null) {
        Log.d(TAG, "Notificacion: " + remoteMessage.getNotification().getBody());

        mostrarNotificacion(
                remoteMessage.getNotification().getTitle(),
                remoteMessage.getNotification().getBody());
    }
    if (remoteMessage.getData().size() > 0){
        Log.d(TAG, "Data: " + remoteMessage.getData());
    }
}

private void mostrarNotificacion(String title, String body ) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);

    String url = "http://acisg.org.br/images/marca.png";
    Bitmap bitmap = null;
    try {
        bitmap = Picasso.with(this)
                .load(url)
                .get();
    }
    catch (IOException e){
        e.printStackTrace();
    }
    //Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.suamarcaaqui);


    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    //noinspection deprecation
    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_stat_name)
                    .setContentTitle(title)
                    .setContentText(body)
                    .setStyle(new NotificationCompat.BigTextStyle().bigText(body))
                    //Imagem grande igual do instagram
                    //.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(myBitmap))
                    .setLargeIcon(bitmap)
                    //.setLargeIcon(myBitmap)
                    .setAutoCancel(true)
                    .setSound(soundUri)
                    .setContentIntent(pendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notificationBuilder.build());
}
}

标签: androidfirebasepush-notificationfirebase-cloud-messagingandroid-notifications

解决方案


将此添加到您的应用程序manifest.xml文件中。当应用程序处于非活动状态时,这将设置通知的小图标。

<meta-data
    android:name="com.google.firebase.messaging.default_notification_icon"
    android:resource="@drawable/ic_stat_ic_notification" />

推荐阅读