在Android上终止应用程序接收系统任务栏通知

时间:2019-06-14 16:41:25

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

当我的应用程序被杀死时,我无法收到我从应用程序服务器设置的通知。 以前我的摇篮是这样的:

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

通过这个gradle配置,即使滑动应用程序,我也可以在前台,后台接收通知

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

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

有了这个新版本,当滑动应用程序时,仍然可以在系统托盘中接收通知吗?如果是这样,有什么想法吗?

2 个答案:

答案 0 :(得分:0)

您是否已在清单中定义了服务类?

<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

答案 1 :(得分:0)

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

我认为除了将应用“白名单”之外,别无其他方法-用户必须手动进行操作,而忘记了Zenfone中的名称。