Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Firebase云消息仅适用于测试通知_Android_Firebase_Kotlin_Firebase Cloud Messaging - Fatal编程技术网

Android Firebase云消息仅适用于测试通知

Android Firebase云消息仅适用于测试通知,android,firebase,kotlin,firebase-cloud-messaging,Android,Firebase,Kotlin,Firebase Cloud Messaging,我最近实现了Firebase云消息传递 我已经能够将测试消息发送到我的测试设备。但是,通过活动发送通知时运气不佳 以下是我如何扩展我的FirebaseMessagingService: class MyFirebaseMessagingService : FirebaseMessagingService() { private val CHANNEL_ID = "YB_NOTIFICATION_CHANNEL" var notification: Notification? =

我最近实现了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());
        }
    }

}
最后,这是清单中声明的服务

        <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云消息传递设置文档本身都没有提到这一点

我想知道,我哪里做错了


此外,即使我的应用程序未打开,我也能够成功地从Notification composer接收目标测试消息。

创建一个扩展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>