【问题标题】:Firebase Cloud messaging only works for test notificationFirebase 云消息传递仅适用于测试通知
【发布时间】:2020-01-04 06:25:44
【问题描述】:

我新实现了 Firebase 云消息传递。

我已经能够向我的测试设备发送测试消息。但是,通过广告系列发送通知并不走运。

这是我如何扩展我的FirebaseMessagingService:


class MyFirebaseMessagingService : FirebaseMessagingService() {
    private val CHANNEL_ID = "YB_NOTIFICATION_CHANNEL"
    var notification: Notification? = null
    private val TAG = "MessagingService"
    private val remote_picture: Bitmap? = null
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        Log.d(TAG, "From: " + remoteMessage.from)
        Log.d(TAG, "Notification Message Body: " + remoteMessage.notification!!.body.toString())
        Log.d(TAG, remoteMessage.from)
        sendNotification(remoteMessage.notification!!.body, remoteMessage.notification!!.title)
    }

    private fun sendNotification(messageBody: String?, title: String?) {
        val intent = Intent(this, NotificationActivity::class.java)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
        val pendingIntent =
            PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        val defaultSoundUri =
            RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALL)
        val icon = BitmapFactory.decodeResource(this.resources, R.drawable.launcher_round)
        val notificationBuilder =
            NotificationCompat.Builder(
                this,
                CHANNEL_ID
            )
                .setAutoCancel(true)
                .setDefaults(Notification.DEFAULT_ALL)
                .setContentText(messageBody)
                .setContentTitle(resources.getString(R.string.app_name))
                .setLargeIcon(icon)
                .setSmallIcon(notificationIcon)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent)
        notification = notificationBuilder.build()
        val manager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        manager.notify(1, notification)
        MyNotificationManager.getInstance(this).displayNotification(title, messageBody)
    }

    private val notificationIcon: Int
        get() {
            val useWhiteIcon = Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
            return if (useWhiteIcon) R.mipmap.mypush else R.mipmap.mypush
        }

}




下面是MyNotificationManager类:


public class MyNotificationManager {

    private Context mCtx;
    private static MyNotificationManager mInstance;

    private MyNotificationManager(Context context) {
        mCtx = context;
    }

    public static synchronized MyNotificationManager getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new MyNotificationManager(context);
        }
        return mInstance;
    }

    public void displayNotification(String title, String body) {

        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(mCtx, AppConstants.CHANNEL_ID)
                        .setSmallIcon(R.mipmap.ic_launcher_round)
                        .setContentTitle(title)
                        .setContentText(body);
        Intent resultIntent = new Intent(mCtx, Notifications.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(mCtx, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        mBuilder.setContentIntent(pendingIntent);

        NotificationManager mNotifyMgr =
                (NotificationManager) mCtx.getSystemService(NOTIFICATION_SERVICE);

        if (mNotifyMgr != null) {
            mNotifyMgr.notify(1, mBuilder.build());
        }
    }

}

最后,这是 Manifest 中声明的服务。

        <service
            android:name="._services.broadcast.MyFirebaseMessagingService"
            android:enabled="true"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

我还搜索了一些关于 SO 的问题,但我看到的是人们提到了 FirebaseInstanceId 以及为此提供的服务。较新的教程甚至 Firebase Cloud Messaging 设置文档本身都没有提到这一点。

我想知道我哪里出错了。

此外,即使我的应用程序未打开,我也能够成功接收来自 Notification composer 的有针对性的测试消息。

【问题讨论】:

    标签: android firebase kotlin firebase-cloud-messaging


    【解决方案1】:

    创建一个类扩展 FirebaseMessagingService

    公共类 FCMessagingService 扩展 FirebaseMessagingService {

    private static final String TAG = "MyFirebaseMsgService";
    
    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        if (remoteMessage.getFrom().equalsIgnoreCase("/topics/update")) {
            sendNotificationUpdate(remoteMessage);
        }
    
    }
    
    @Override
    public void onNewToken(String token) {
        sendRegistrationToServer(token);
    }
    
    
    private void sendRegistrationToServer(String token) {
        Hawk.put("fcm_token", token);
    }
    
    private void sendNotificationUpdate(RemoteMessage messageBody) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://***.***.***.***"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 1001, browserIntent,
                PendingIntent.FLAG_ONE_SHOT);
    
        String channelId = getString(R.string.default_notification_channel_id);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.ic_notifications_black_24dp)
                        .setContentTitle(messageBody.getNotification().getTitle())
                        .setContentText(messageBody.getNotification().getBody())
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .addAction(new NotificationCompat.Action(R.drawable.ic_cloud_download_black_24dp, "دانلود", pendingIntent))
                        .setContentIntent(pendingIntent);
    
        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "update",
                    NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
        }
    
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }
    

    }

    在文件 AndroidManifest.xml 和应用程序标签中使用以下代码:

    <meta-data
            android:name="com.google.firebase.messaging.default_notification_icon"
            android:resource="@drawable/ic_notifications_black_24dp" />
    
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_color"
            android:resource="@color/colorAccent" />
        <meta-data
            android:name="com.google.firebase.messaging.default_notification_channel_id"
            android:value="@string/default_notification_channel_id" />
        <service
            android:name=".Services.FCMessagingService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
    

    【讨论】:

      猜你喜欢
      • 2019-06-12
      • 2021-07-22
      • 2016-12-25
      • 2020-04-12
      • 2020-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多