当应用程序不在后台运行时,推送通知未收到



我已将 Firebase Cloud Messaging 添加到我的应用中,并设置了 Firebase 消息服务。它工作正常,即使应用程序不在后台运行,应用程序也会收到通知。但从最近 2 天开始,应用程序没有收到任何通知。我不知道我的代码发生了什么。

Fcm消息服务.java

import android.annotation.SuppressLint;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Build;
import androidx.core.app.NotificationCompat;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class FcmMessagingService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
if (remoteMessage.getData().size()>0){
String title,message,img_url;
title = remoteMessage.getData().get("title");
message = remoteMessage.getData().get("message");
img_url = remoteMessage.getData().get("img_url");
Intent intent = new Intent(this, Notification.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
Bitmap rawBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.app_icon_round);
Uri sounduri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
String id = "MY_CH_ID";
CharSequence name = "Product";
String description = "Notifications regarding our products";
int importance = NotificationManager.IMPORTANCE_MAX;
@SuppressLint("WrongConstant")
NotificationChannel mChannel = new NotificationChannel(id, name, importance);
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.setSound(sounduri,attributes);
notificationManager.createNotificationChannel(mChannel);
}
final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MY_CH_ID");
builder.setContentTitle(title);
builder.setSubText(message);
builder.setContentIntent(pendingIntent);
builder.setSound(sounduri);
builder.setSmallIcon(R.drawable.fcmicon);
builder.setLargeIcon(rawBitmap);
builder.setChannelId("MY_CH_ID");

ImageRequest imageRequest = new ImageRequest(img_url, new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(response));
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0,builder.build());
}
}, 0, 0, null, Bitmap.Config.RGB_565, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
MySingleton.getmInstance(this).addToRequestQue(imageRequest);
}
}
}

在我的主要活动中,我订阅了一个主题。

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

安卓清单.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.parethumukal">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/app_icon_round"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".Archived"
android:configChanges="orientation"
android:screenOrientation="portrait" ></activity>
<activity android:name=".Ebook"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".Prayer"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".Churchtiming"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Forgotpassword"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Privacypolicy"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Directoryquick"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Churchinside"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Extras"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Shrine"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".Youtubeplayer" />
<activity
android:name=".Video"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Driveactivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Audios"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Images"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Profileregistration"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Registration"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Milandetails"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Ambulance"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Milan"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Feast"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Priest"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Churchadmin"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Youthassociation"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Vanithasamajam"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Familyunit"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Sundayschool"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Leaders"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".History"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity android:name=".Livestream" />
<activity
android:name=".Notification"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Myprofile"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Quicklinks"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Services"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Directory"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Gallery"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Groups"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Administration"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Church"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Aboutus"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".WelcomeActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Homewindow"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Login"
android:configChanges="orientation"
android:screenOrientation="portrait" />
<activity
android:name=".Splashscreen"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".FcmMessagingService"
android:stopWithTask="false">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
</application>
</manifest>

build.gradle 实现

implementation 'com.google.firebase:firebase-messaging:19.0.1'

我不知道问题出在哪里。

你这边一切都很好。您只需要发送Firebase 通知消息即可,请参阅此处。

如您所见,NotificationJSON 对象,它包括将在通知标题中显示的titlebody键。

样本:

{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}

不要忘记将token替换为您的 FCM 设备令牌。

相关内容

  • 没有找到相关文章

最新更新