【问题标题】:Receive system tray notification when app killed on Android当应用程序在 Android 上被杀死时接收系统托盘通知
【发布时间】:2019-06-14 16:41:25
【问题描述】:

当我的应用被终止时,我无法从我的应用服务器接收我设置的通知。 以前我的 gradle 是这样的:

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

通过这个 gradle 配置,我能够在前台、后台接收通知,即使在应用被滑动时也是如此

我更新了我的 forebase-messanging,所以这是我使用 firebase-messanging 的新 gradle:

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

这是我当前更新后的服务类

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

在这个新版本中,当应用被刷卡时,是否仍然可以在系统托盘中接收通知?如果是这样,有什么想法吗?

【问题讨论】:

  • 我注意到有时在您从服务器发送通知到您在应用程序中收到通知之间会有延迟。尝试等待一段时间并再次发送通知。版本更改不应影响通知的行为。
  • @Dinesh 我不认为这是一个时间问题,不管通知仍然没有来

标签: android push-notification firebase-cloud-messaging


【解决方案1】:

您是否像这样在清单中定义了服务类?:

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

如果在此之后问题仍然存在。添加以下方法并从您的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());
}

以上方法取自Firebase repository

【讨论】:

  • 感谢您的反馈,但事情是 onMessageReceived 甚至没有被调用...日志什么也没显示。我尝试了您的建议,但当我滑动应用时仍然无法收到通知
【解决方案2】:

我在某些手机上也遇到过同样的情况,例如华硕 Zenfone。当我从当前任务/应用程序列表中滑动应用程序时,Firebase 消息未传递,所有后台服务/作业都停止/从不触发。但在三星等其他制造商上,滑动应用程序仍然可以接收这些消息。

我认为除了将您的应用程序“列入白名单”之外别无他法——用户必须手动执行此操作,忘记了 Zenfone 中的名称。

【讨论】:

    猜你喜欢
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-21
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多