应用被杀死后的推送通知 [英] Push Notifications after App is killed

查看:236
本文介绍了应用被杀死后的推送通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Android上将应用程序杀死后,我似乎无法收到从FCM控制台发送的FCM推送通知,就像长按概述"按钮并滑动要杀死的应用程序一样.当应用程序在前台或后台运行时,它绝对可以正常工作.这似乎是一个重复的问题,但是我尝试了其他方法,但仍然无法理解.

I can't seem to receive FCM Push Notifications that I send from the FCM console after app is killed on android, as in long-press the Overview button and swiping the app to be killed. It works absolutely fine when the app is running in the foreground or background. This may seem like a duplicate question but I have tried the other methods but I still cannot seem to get it.

NotificationService.java

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

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.

}

}

MainActivity.java

MainActivity.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());


}

}

推荐答案

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

I had exactly the same problem. Notifications showed just fine in foreground and background mode, but not when the app was killed. Eventually I found this: https://github.com/firebase/quickstart-android/issues/41#issuecomment-306066751

问题出在Android Studio中的调试模式上.要正确测试您的通知,请执行以下操作: 在Android Studio中通过调试运行您的应用. 轻扫即可杀死. 通过手机上的启动器图标重新启动应用. 再次将其擦去(使其被杀死).

The problem is with the debugging mode in Android Studio. To correctly test your notifications do the following: Run your app via debug in Android Studio. Swipe it away to be killed. Restart the app via the launcher icon on your phone. Swipe it away again (so it gets killed).

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

Send your notification and now you will see it!

希望这可以解决您的问题.

Hope this solved your problem.

这篇关于应用被杀死后的推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆