[英]Android app not showing push notification
我的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");
}
}
}
广播接收器必须实现
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.