我的Android应用程序在前台时收到通知,但在后台时不显示通知。
我调试了代码,它正确无误地通过并记录了所有内容,但没有任何通知。
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (Foreground.getInstance().isForeground()) {
Intent callIntent = new Intent(getApplicationContext(), CallActivity.class);
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
} else {
Log.d(TAG, "Creating notification");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext(), "0")
.setSmallIcon(R.drawable.ic_notification)
.setColor(getResources().getColor(R.color.fcmNotificationBg))
.setContentTitle("Title")
.setContentText("Some text")
.setAutoCancel(true);
Intent intent = new Intent(getApplicationContext(), CallActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(CallActivity.class);
stackBuilder.addNextIntent(intent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
notificationBuilder.setContentIntent(resultPendingIntent);
Log.d(TAG, "Creating notificationManager");
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
if (notificationManager != null) {
notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
Log.d(TAG, "notify called");
} else
Log.d(TAG, "notificationManager is NULL");
}
}
}
答案 0 :(得分:0)
必须实现广播接收器
public class SensorRestarterBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent i = new Intent();
i.setClass(context, MyFirebaseMessagingService.class);
context.startService(i);
}
}
清单中
<receiver
android:name=".services.SensorRestarterBroadcastReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
</intent-filter>
</receiver>