我在Android studio中使用Firebase为我的App创建了一个通知系统。 该应用程序有一个小缺陷。
当应用程序在屏幕上可见时,它正常工作,图标显示为应该和大图像。请参见下面的屏幕截图。
当关闭应用程序并发送通知时,会出现崩溃,图标显示为球,并且不会显示大图像。请参见下面的屏幕截图。
我将发布我正在使用的代码。如果有人已经通过这个并且可以帮助我,我将非常感激。
的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>
My Class 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());
}
}
答案 0 :(得分:1)
将此添加到您的应用程序manifest.xml
文件中。这将在应用处于非活动状态时设置通知的小图标。
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_stat_ic_notification" />