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

时间:2019-08-08 01:33:54

标签: android firebase-cloud-messaging

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

FcmMessagingService.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");

AndroidManifest.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'

我不知道出什么问题了。

1 个答案:

答案 0 :(得分:1)

一切都对您有利。您只需要发送 Firebase通知消息 see here

您将看到Notification JSON对象,它包含titlebody键,这些键将显示在通知标题中。

示例:

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

别忘了用您的FCM设备令牌替换token