[英]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.