Firebase 通知,图标和大图像中的小故障
Posted
技术标签:
【中文标题】Firebase 通知,图标和大图像中的小故障【英文标题】:Firebase notification with small failure in the icon and large image 【发布时间】:2018-05-04 18:07:21 【问题描述】:我在 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);
My Class 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());
【问题讨论】:
你能把日志贴出来吗? 【参考方案1】:将此添加到您的应用程序manifest.xml
文件中。这将在应用处于非活动状态时设置通知的小图标。
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />
【讨论】:
以上是关于Firebase 通知,图标和大图像中的小故障的主要内容,如果未能解决你的问题,请参考以下文章
使颤动通知(通过firebase)弹出到前台(并且不仅仅是状态栏中的图标)