如何在android中自定义通知

我刚刚使用主题消息传递实现了 firebase 云消息传递 pushNotification。收到通知,但 MyFirebaseMessagingService 未正常工作。它传递消息的方式与云消息传递相同。


完成云消息功能。创建一个名为 MyFirebaseMessagingService 的服务并使用 FirebaseMessagingService 进行扩展。将服务添加到清单。


这是我的 FirebaseMessagingService 类。


@Override

public void onDeletedMessages() {

    super.onDeletedMessages();

}


@Override

public void onMessageReceived(RemoteMessage remoteMessage) {

    super.onMessageReceived(remoteMessage);


    String messageTitle = remoteMessage.getNotification().getTitle();

    String clickAction = remoteMessage.getNotification().getClickAction();

    Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"+ getApplicationContext().getPackageName() + "/" + R.raw.hollow);


    NotificationCompat.Builder builder = new NotificationCompat.Builder(this, getString(R.string.default_notification_channel_id))

            .setSmallIcon(R.mipmap.ic_launcher_round)

            .setContentTitle("New Notification")

            .setSound(soundUri)

            .setContentText(messageTitle);


    Intent resultIntent = new Intent(clickAction);


    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    builder.setContentIntent(pendingIntent);

    builder.setPriority(Notification.PRIORITY_HIGH);

    if (Build.VERSION.SDK_INT >= 21) builder.setVibrate(new long[0]);


    int mNotificationId = (int) System.currentTimeMillis();


    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);


    notificationManager.notify(mNotificationId, builder.build());


}

我的清单文件:-


<service

            android:name=".MyFirebaseMessagingService"

            android:exported="true">

            <intent-filter>

            <action android:name="com.google.firebase.MESSAGING_EVENT"/>

            </intent-filter>

        </service>

        <meta-data

            android:name="com.google.firebase.messaging.default_notification_channel_id"

            android:value="@string/default_notification_channel_id" />


慕斯王
浏览 115回答 1
1回答

慕村225694

在发送云消息时,您还必须订阅要接收通知的主题,例如。FirebaseMessaging.getInstance().subscribeToTopic("general")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .addOnCompleteListener(task -> {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task.isSuccessful())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, "Subs-Successful", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(this, "Subs NOT Successful", Toast.LENGTH_SHORT).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });在此,我订阅了常规主题,并且只会收到关于该主题发送的消息,如下所示。希望这有帮助......对我有用!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java