android 杀进程推送服务,android – 应用程序被杀后推送通知

博客内容提到,在Android应用被杀死后,无法接收到FCM(Firebase Cloud Messaging)推送通知。问题在于调试模式下。为正确测试通知,需在Android Studio中通过调试运行应用,然后正常关闭和重启应用,再发送通知,这样可以接收到通知。解决方案与FCM服务和通知的处理有关。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

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

}

}

解决方法:

问题在于Android Studio中的调试模式.要正确测试您的通知,请执行以下操作:

在Android Studio中通过调试运行您的应用.

滑动即可将其杀死.

通过手机上的启动器图标重新启动应用.

再次滑动它(因此它被杀死).

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

希望这能解决你的问题.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值