当应用程序处于后台时,Firebase 通知图标不会出现
Posted
技术标签:
【中文标题】当应用程序处于后台时,Firebase 通知图标不会出现【英文标题】:Firebase Notification Icon is not coming when App is in background 【发布时间】:2018-02-26 04:49:11 【问题描述】:如果我从 Firebase 生成通知,通知图标不会出现。(应用程序在后台)。如果应用程序在前台,图标就会出现。
经过大量研究,我才知道这一点。
如果应用程序在后台,它不会调用OnmessageReceivedmethod();
所以为此我们必须覆盖OnHandleIntent()
方法,但我无法覆盖它。为什么,因为它是最终的..
请给我一些解决方案?
【问题讨论】:
你试过这个***.com/a/37845174/3789481 吗? 请检查您的代码,可能是您编写了一些停止服务的内容。 FCM 代表客户端应用自动向最终用户设备显示消息。通知消息具有一组预定义的用户可见键。 【参考方案1】:[![enter image description here][1]][1]
import android.app.ActivityManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.text.html;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
public class FirebaseMessagingReceiveService extends FirebaseMessagingService
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
private String the_message = "";
private String the_title = "ALERT_TITLE";
public static final String TAG = "FCM Demo";
private Context mContext;
@Override
public void onMessageReceived(RemoteMessage remoteMessage)
if (remoteMessage == null)
return;
// 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());
private void handleDataMessage(JSONObject json)
Log.e(TAG, "push json: " + json.toString());
try
the_title = json.getString("DgTitle");
the_message = json.getString("DgMessage");
sendNotification(the_message, the_title);
catch (JSONException e)
Log.e(TAG, "Json Exception: " + e.getMessage());
catch (Exception e)
Log.e(TAG, "Exception: " + e.getMessage());
private void sendNotification(String msg, String theTitle)
mContext = getApplicationContext();
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> services = activityManager
.getRunningTasks(Integer.MAX_VALUE);
boolean isActivityFound = false;
if (services.get(0).topActivity.getPackageName().toString()
.equalsIgnoreCase(getPackageName().toString()))
isActivityFound = true;
Intent openIntent = null;
if (isActivityFound)
openIntent = new Intent();
else
openIntent = new Intent(this, MainActivity.class);
openIntent.putExtra("Title", the_title);
openIntent.putExtra("Message", the_message);
openIntent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
openIntent, PendingIntent.FLAG_ONE_SHOT);
if (msg != null && (!msg.isEmpty()))
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setDefaults(Notification.DEFAULT_ALL)
.setVibrate(new long[]100, 250, 100, 250, 100, 250)
.setAutoCancel(true)
.setColor(getResources().getColor(R.color.activity_toolbar_color))
.setContentTitle(theTitle)
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(Html.fromHtml(msg)))
.setPriority(Notification.PRIORITY_MAX)
.setContentText(Html.fromHtml(msg));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
mBuilder.setSmallIcon(R.drawable.notification_icon1);
else
mBuilder.setSmallIcon(R.drawable.notification_icon);
mBuilder.setContentIntent(contentIntent);
SharedPreferences prefs = mContext.getSharedPreferences(MainActivity.class.getSimpleName(), Context.MODE_PRIVATE);
int notificationNumber = prefs.getInt("notificationNumber", 0);
// create unique id for each notification
mNotificationManager.notify(notificationNumber, mBuilder.build());
SharedPreferences.Editor editor = prefs.edit();
if (notificationNumber < 5000)
notificationNumber++;
else
notificationNumber = 0;
editor.putInt("notificationNumber", notificationNumber);
editor.commit();
Log.d("notificationNumber", "" + notificationNumber);
this help to start FCM service in background when phone start.
>
> Add receiver in Manifest.xml
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name=".OnBootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
> OnBootBroadcastReceiver.class
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class OnBootBroadcastReceiver extends BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent)
Intent i = new Intent("com.examle.FirebaseMessagingReceiveService");
i.setClass(context, FirebaseMessagingReceiveService.class);
context.startService(i);
> Add following dependency in app gradle.
//Used for firebase services
compile 'com.google.firebase:firebase-core:11.8.0'
compile 'com.google.firebase:firebase-messaging:11.8.0'
[1]: https://i.stack.imgur.com/lcCZK.png
【讨论】:
jaspreet Kaur 对我没用。它仍然显示灰色背景,正方形而不是通知 请查看此问题***.com/questions/39157134/… 请注意 notification_icon1 需要是 144 X 144 PNG 32 位颜色 notification_icon 需要是 48 X 48 PNG 32 位颜色 144×144 尺寸太大。对于 notification_icon1 和 notification_icon 是 48×48 如果我设置相同大小的图标,它肯定会工作。请确认您的代码在前台和后台都适用。 是的,代码正在运行,问题出在 notification_icon1.. 检查并告诉我【参考方案2】:请将此图标用于通知图标 1
【讨论】:
1.调整图片大小 144 X 144 PNG 2. 然后使用以下链接创建透明背景图片www140.lunapic.com/editor 我试过你的图片它不起作用..如果应用程序在后台,它甚至不会去 OnmessageReceived() 方法 你添加了我的 1 个回答中给出的 BroadcastReceiver【参考方案3】:如果您在设置图标大小时遇到困难,请使用 android studio 3.0.1 中的 android studio 的图像资产制造商,它也允许生成通知图标。
同时尝试让你的图标为单一颜色。
同时设置firebase通知的默认图标:
<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@drawable/ic_notification_default" />
【讨论】:
以上是关于当应用程序处于后台时,Firebase 通知图标不会出现的主要内容,如果未能解决你的问题,请参考以下文章
当应用程序打开或应用程序处于后台时,Phonegap Firebase 推送通知不会触发事件侦听器
当应用程序被杀死(非活动)时,点击 Firebase 通知不起作用