简体   繁体   English

Android应用未显示推送通知

[英]Android app not showing push notification

My Android app receive notification when foreground but doesn't show notification when background. 我的Android应用在前台收到通知,但在后台收到通知。

I debugged code and it goes through correctly without errors and logs everything but no notification remaining. 我调试了代码,它正确无误地通过并记录了所有内容,但没有剩余通知。

 @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");
        }
    }
}

Broadcast receiver has to be implemented 广播接收器必须实现

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);
    }
}

In manifest 在清单中

<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.

相关问题 当应用未在android中运行时,推送通知未显示 - Push Notification is not showing when app is not running in android 在Android应用中推送通知 - push notification in android app Android推送通知消息应用程序图标和标题未显示 - Android push notification message app icon and title not showing 混合应用推送通知图标未在Android 8(Oreo)上显示 - Hybrid app push notification icon not showing on Android 8 (Oreo) Android Firebase推送通知未显示 - Android Firebase push notification not showing 推送通知未显示在 Android 前台 - Push notification not showing in Android foreground 带有来自服务器(php)的图像的 Android Firebase 推送通知未在通知中显示图像,而应用程序在后台 - Android Firebase Push Notification with image from server(php) not showing image in notification while app in background 在Android应用中实现推送通知 - implement push notification in android app 通过GCM推送Android应用的通知 - Push notification for android app by GCM 设备上没有通知,但应用程序中有通知-Android推送通知 - no notification on device but there is notification in app -Android push notifications
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM