日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

android 程序运行不了,当应用程序在后台运行或不运行时,Android – 推送通知工作不正确...

發(fā)布時間:2025/4/5 Android 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 程序运行不了,当应用程序在后台运行或不运行时,Android – 推送通知工作不正确... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

我使用Firebase Cloud Messaging發(fā)送推送通知.

這是我的FirebaseMessageService:

public class FireBaseMessageService extends FirebaseMessagingService {

@Override

public void onMessageReceived(RemoteMessage remoteMessage) {

Log.e("TAG", "From: " + remoteMessage.getFrom());

Log.e("TAG", "Notification Message Body: " + remoteMessage.getData().get("CardName")+" : "+remoteMessage.getData().get("CardCode"));

sendNotification(remoteMessage.getNotification().getBody());

}

private void sendNotification(String messageBody) {

Intent intent = new Intent(this, StartActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,

PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)

.setSmallIcon(R.mipmap.ic_launcher_final)

.setContentTitle("Notification")

.setContentText(messageBody)

.setTicker("Test")

.setAutoCancel(true)

.setDefaults(Notification.DEFAULT_SOUND)

.setContentIntent(pendingIntent);

NotificationManager notificationManager =

(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());

}

}

和FirebaseInstanceServer:

public class FirebaseInstanceService extends FirebaseInstanceIdService {

@Override

public void onTokenRefresh() {

// Get updated InstanceID token.

String refreshedToken = FirebaseInstanceId.getInstance().getToken();

Log.e("TAG", "Refreshed token: " + refreshedToken);

// TODO: Implement this method to send any registration to your app's servers.

sendRegistrationToServer(refreshedToken);

}

private void sendRegistrationToServer(String token) {

// Add custom implementation, as needed.

Log.e("TAG", "Refreshed token2: " + token);

}

}

哪個在AndroidManifest中聲明:

android:name=".util.notifications.FireBaseMessageService">

android:name=".util.notifications.FirebaseInstanceService">

所以問題是,當(dāng)應(yīng)用程序運(yùn)行的代碼顯示得很好,通知有默認(rèn)聲音,但是當(dāng)應(yīng)用程序在前臺或沒有運(yùn)行通知沒有任何聲音和代碼未顯示在狀態(tài)欄.

為什么會發(fā)生這種情況,我該如何解決?

總結(jié)

以上是生活随笔為你收集整理的android 程序运行不了,当应用程序在后台运行或不运行时,Android – 推送通知工作不正确...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。