应用程序被杀死后推送通知

新手上路,请多包涵

我似乎无法收到我在 android 上终止应用程序后从 FCM 控制台发送的 FCM 推送通知,就像长按“概览”按钮并滑动要终止的应用程序一样。当应用程序在前台或后台运行时,它工作得非常好。这似乎是一个重复的问题,但我已经尝试了其他方法,但我似乎仍然无法理解。

通知服务.java

 public class NotificationService extends FirebaseMessagingService
{

private static final String TAG = "FCM Service";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {

    Log.d(TAG, "From: " + remoteMessage.getFrom());
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
    sendNotification(remoteMessage.getNotification().getBody());
}

private void sendNotification(String messageBody) {

        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        android.support.v4.app.NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Firebase")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

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

        notificationManager.notify(0, notificationBuilder.build());
}
}

TokenRefresh.java

 public class TokenRefresh extends FirebaseInstanceIdService {

private static final String TAG = "FirebaseIDService";

@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

    // TODO: Implement this method to send any registration to your app's servers.
    sendRegistrationToServer(refreshedToken);
}

/**
 * Persist token to third-party servers.
 *
 * Modify this method to associate the user's FCM InstanceID token with any server-side account
 * maintained by your application.
 *
 * @param token The new token.
 */
private void sendRegistrationToServer(String token) {
    // Add custom implementation, as needed.

}

}

主活动.java

 public class MainActivity extends AppCompatActivity {

Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FirebaseMessaging.getInstance().subscribeToTopic("global");

    String token = ("fcm"+ FirebaseInstanceId.getInstance().getToken());

}

}

原文由 Darsshan 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.3k
2 个回答

您需要标记您的 Firebase 服务类,以免与您的主应用程序一起被杀死。

     ComponentName componentName = new ComponentName(
            applicationContext,
            FCMService.class);

    applicationContext.getPackageManager().setComponentEnabledSetting(
            componentName,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP);

你可以 在 Java 中检查这个真实的实现

原文由 Rafael Setragni 发布,翻译遵循 CC BY-SA 4.0 许可协议

我有完全相同的问题。通知在前台和后台模式下显示得很好,但在应用程序被终止时就没有了。最终我发现了这个: https ://github.com/firebase/quickstart-android/issues/41#issuecomment-306066751

问题在于 Android Studio 中的调试模式。要正确测试您的通知,请执行以下操作: 在 Android Studio 中通过调试运行您的应用。将其滑开即可被杀死。通过手机上的启动器图标重新启动应用程序。再次将其滑开(因此它会被杀死)。

发送您的通知,现在您将看到它!

希望这能解决您的问题。

原文由 iDoes 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题