简体   繁体   English

在Android上杀死应用时收到系统托盘通知

[英]Receive system tray notification when app killed on Android

When my app gets killed, I can't receive notifications I set from my app server. 当我的应用程序被杀死时,我无法收到我从应用服务器设置的通知。 Previous my gradle was as such: 以前我的gradle是这样的:

implementation "com.google.firebase:firebase-messaging:17.0.0"
implementation "com.google.firebase:firebase-invites:16.0.0"

Wih this gradle config I was able to receive notification in foreground, background even when app is swiped 通过这个gradle配置,我能够在前台,后台接收通知,即使应用程序被刷过

I updated my forebase-messanging so here's my new gradle with firebase-messanging: 我更新了我的forebase-messanging所以这是我的新gradle with firebase-messanging:

implementation "com.google.firebase:firebase-messaging:18.0.0"

Here's my current Service class after update 这是我更新后的当前服务类

public class MyService extends FirebaseMessagingService
{
public static final String ACTION = "action";
public static final String EXTRA_DATA = "extra_data";
public static final String MESSAGE = "message";
private RemoteMessage.Notification mNotification;

@Override
public void onNewToken(String refreshedToken) {
    super.onNewToken(refreshedToken);
    LOGE(TAG, "Refreshed token: " + refreshedToken);

}

@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    String from = message.getFrom();
    mNotification = message.getNotification();
    Map<String, String> data = message.getData();
    LOGE(TAG, "Received FCM message from : " + from + " data : " + data);
}
}

With this new version, it it still possible to receive notifications in system-tray when app is swiped ? 使用这个新版本,当刷新应用程序时,它仍然可以在系统托盘中接收通知吗? If so, any ideas ? 如果是这样,任何想法?

Have you defined service class in your manifest like this?: 您是否在此清单中定义了服务类?:

<service
    android:name=".MyService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

and if the problem persists even after that. 如果问题在此之后仍然存在 add below method and call it from your onMessageReceived THis should resolve any kind of problems. 添加以下方法并从您的onMessageReceived调用它应解决任何类型的问题。

    private void sendNotification(String messageBody) {
        Intent intent = new Intent(this, MainActivity.class); // Put your own launcher Activity
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_ONE_SHOT);

        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_stat_ic_notification)
                        .setContentTitle(getString(R.string.fcm_message))
                        .setContentText(messageBody)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }

        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
}

The above method is taken from Firebase repository . 上述方法取自Firebase存储库

I have experienced the same on certain phones eg Asus Zenfone. 我在某些手机上也遇到过同样的情况,例如Asus Zenfone。 When I swipe the app from the current tasks/apps list, firebase messages are not delivered, all background services/jobs are stopped/never triggered. 当我从当前任务/应用列表中滑动应用时,不会传递firebase消息,所有后台服务/作业都会停止/永不触发。 But on other manufacturers like Samsung, the swiping the app still can receive those messages. 但对于像三星这样的其他制造商来说,刷这个应用程序仍然可以接收这些消息。

I think there is no other way except of "whitelisting" your app - user has to do it manually, forgot what it's called in Zenfone. 我认为除了“将你的应用程序列入白名单”之外别无他法 - 用户必须手动完成,忘记Zenfone中的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当应用程序从Ionic 2中的应用程序托盘中被杀死时,如何继续接收GCM通知 - how to continue to receive GCM notification when app killed from app tray in Ionic 2 使用 FCM 杀死应用程序时,Android 无法接收通知 - Android unable to receive notification when app is killed using FCM 当从联想等某些设备上的多任务托盘中杀死应用程序时,Android应用程序未收到Firebase推送通知 - Android app not receiving Firebase Push Notification when application is killed from multi-task tray on some devices like lenovo 应用终止后未触发Android通知 - Android notification not fired when app killed 当应用程序被终止时,通知未在 Android 8 及更高版本中显示 - Notification not showing in Android 8 and above when app is killed 未从通知托盘中获取捆绑包中的 Pubnub 有效负载。 当应用程序被杀死时 - Not getting Pubnub payload in bundle from Notification tray. When app is killed 处理在Android系统托盘中解除的Firebase通知 - Handle dismissed Firebase notification in Android System Tray Android推送通知系统任务栏图标为灰色 - Android Push Notification System Tray Icon Is Gray android 系统托盘中的通知,不起作用 - Notification in android system tray, does not work 应用程式被杀死时,Android无法接收到解析推送通知 - Unable tor receive parse push notifications when app is killed Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM