【问题标题】:Customize firebase notification自定义 Firebase 通知
【发布时间】:2017-05-12 10:34:13
【问题描述】:

我有一个聊天应用程序,其中用户订阅了一个主题,每个组都是一个主题。每当在组中发送消息时。将向该主题发送通知。

我面临两个问题。

  1. 当发件人在群组中发送消息时,会向主题发送一条通知消息。但在用户从 firebase 收到通知之前,他关闭了应用程序或应用程序进入后台。所以根据firebase文档,通知被发送到通知托盘而不是onMessageReceived回调。

  2. 从 Firebase 收到的通知被添加到托盘中。发件人以外的用户如何获得通知 ID,以便在需要时取消我。如何自定义此通知?

有没有办法在应用程序处于后台或终止时保持活动侦听器以接收通知。

请帮忙

【问题讨论】:

    标签: android firebase android-notifications


    【解决方案1】:

    您可能想看看this。起初,由于英语不是我的主要语言,我在阅读文档时总是遇到问题。这很令人困惑,但只需按照步骤进行操作,您就会更加了解。

    对于您的第一个问题,您不需要同时使用通知和数据消息。如果您使用它,它将阻止onMessageReceived() 在应用程序处于前台或强制关闭时拨打电话。相信我,只需删除通知{notification:"data"},但在发送到 firebase 时保留{data:"something"}。它总是会触发onMessageReceived()

    对于您按照上述步骤操作后的第二个问题,您的状态栏上不会显示任何通知。在这里您可以检查该用户是否是发件人,如果不是发件人,那么您可以在 onMessageReceived() 中显示您的自定义通知。

    【讨论】:

      【解决方案2】:

      您可以通过发送通知向您的应用推送通知,以便用户与火基地互动,当您的应用关闭时,根据一些参数转到火基地。

      确保在执行此操作之前先将您的项目添加到 Fire base 中:否则将您的项目添加到 fire base 中,并在您的项目的 app 文件夹中使用包名、指纹和 google_services.json 文件。

      Fire base Cloud Messaging

      它会将通知推送到您的应用程序,如果它关闭则它让用户通过按下通知打开应用程序,如果您想向用户显示通知以定向到同一帐户的另一个应用程序,当应用程序将使用这两种代码:

      为您创建一流的MyFirebaseMessagingService

      public class MyFirebaseMessagingService extends FirebaseMessagingService {
      
      private static final String NOTIFICATION_ID_EXTRA = "notificationId";
      private static final String IMAGE_URL_EXTRA = "imageUrl";
      private static final String ADMIN_CHANNEL_ID ="admin_channel";
      private NotificationManager notificationManager;
      
      
      @Override
      public void onMessageReceived(RemoteMessage remoteMessage) {
      
          if (remoteMessage.getData().size()>0){
      
              Intent notificationIntent = new Intent(Intent.ACTION_VIEW);
              notificationIntent.setData(Uri.parse(remoteMessage.getData().get("applink")));
              PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, 0);
              notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
      
              final PendingIntent pendingIntent = PendingIntent.getActivity(this,
                      0 /* Request code */, notificationIntent,
                      PendingIntent.FLAG_ONE_SHOT);
      
              int notificationId = new Random().nextInt(60000);
              Bitmap bitmap = getBitmapfromUrl(remoteMessage.getData().get("imageurl"));
      
              Intent likeIntent = new Intent(this,LikeService.class);
              likeIntent.putExtra(NOTIFICATION_ID_EXTRA,notificationId);
              likeIntent.putExtra(IMAGE_URL_EXTRA,remoteMessage.getData().get("message"));
              PendingIntent likePendingIntent = PendingIntent.getService(this,
                      notificationId+1,likeIntent, PendingIntent.FLAG_ONE_SHOT);
      
              Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
      
              notificationManager =
                      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      
              if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
      
                  setupChannels();
      
              }
      
              NotificationCompat.Builder notificationBuilder =
                      new NotificationCompat.Builder(this, ADMIN_CHANNEL_ID)
                              .setLargeIcon(bitmap)
                              .setSmallIcon(R.mipmap.ic_launcher)
                              .setContentTitle(remoteMessage.getData().get("title"))
                              .setStyle(new NotificationCompat.BigPictureStyle()
                                      .setSummaryText(remoteMessage.getData().get("message"))
                                      .bigPicture(bitmap))/*Notification with Image*/
                              .setContentText(remoteMessage.getData().get("message"))
                              .setAutoCancel(true)
                              .setSound(defaultSoundUri)
                              .addAction(R.drawable.icon,
                                      getString(R.string.notification_add_to_cart_button),likePendingIntent)
                              .setContentIntent(pendingIntent);
      
              notificationManager.notify(notificationId, notificationBuilder.build());
      
      
          }
      
      }
      
      
      @RequiresApi(api = Build.VERSION_CODES.O)
      private void setupChannels(){
          CharSequence adminChannelName = getString(R.string.notifications_admin_channel_name);
          String adminChannelDescription = getString(R.string.notifications_admin_channel_description);
      
          NotificationChannel adminChannel;
          adminChannel = new NotificationChannel(ADMIN_CHANNEL_ID, adminChannelName, NotificationManager.IMPORTANCE_LOW);
          adminChannel.setDescription(adminChannelDescription);
          adminChannel.enableLights(true);
          adminChannel.setLightColor(Color.RED);
          adminChannel.enableVibration(true);
          if (notificationManager != null) {
              notificationManager.createNotificationChannel(adminChannel);
          }
      }
      
      
      
      public Bitmap getBitmapfromUrl(String imageUrl) {
          try {
              URL url = new URL(imageUrl);
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
              connection.setDoInput(true);
              connection.connect();
              InputStream input = connection.getInputStream();
              return BitmapFactory.decodeStream(input);
      
          } catch (Exception e) {
              e.printStackTrace();
              return null;
          }
      }
      
      
          }
      

      再创建一个类FirebaseIDService来获取fire base的实例id服务

      public class FirebaseIDService extends FirebaseInstanceIdService {
      
      
      public static final String FIREBASE_TOKEN = "firebase token";
      
      @Override
      public void onTokenRefresh() {
          super.onTokenRefresh();
      
          String refreshedToken = FirebaseInstanceId.getInstance().getToken();
          SharedPreferences preferences =
                  PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
          preferences.edit().putString(FIREBASE_TOKEN, refreshedToken).apply();
      
      }
      

      制作类名LikeService

      public class LikeService extends Service {
      
      private static final String NOTIFICATION_ID_EXTRA = "notificationId";
      private static final String IMAGE_URL_EXTRA = "imageUrl";
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
          }
      

      要使用 Firebase 在 Oreo 上支持通知,请不要忘记创建频道,并且此频道会在 您的第一个启动器活动中初始化。

      在您的项目的第一个启动器活动的 oncreate 中包括这些渠道;

         String channelId = "1";
          String channel2 = "2";
      
          NotificationManager notificationManager =
                  (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      
          if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
              NotificationChannel notificationChannel = new NotificationChannel(channelId,
                      "Channel 1", NotificationManager.IMPORTANCE_HIGH);
      
              notificationChannel.setDescription("This is BNT");
              notificationChannel.setLightColor(Color.RED);
              notificationChannel.enableVibration(true);
              notificationChannel.setShowBadge(true);
              notificationManager.createNotificationChannel(notificationChannel);
      
              NotificationChannel notificationChannel2 = new NotificationChannel(channel2,
                      "Channel 2",NotificationManager.IMPORTANCE_MIN);
      
              notificationChannel.setDescription("This is bTV");
              notificationChannel.setLightColor(Color.RED);
              notificationChannel.enableVibration(true);
              notificationChannel.setShowBadge(true);
              notificationManager.createNotificationChannel(notificationChannel2);
      
          }
      

      现在您必须将您的 Firebase 服务类放在 Mainfest 应用程序标签下:

            <service android:name=".activities.services.MyFirebaseMessagingService"
              android:permission="com.google.android.c2dm.permission.SEND">
              <intent-filter>
      
                  <action android:name="com.google.firebase.MESSAGING_EVENT"/>
                  <action android:name="com.google.android.c2dm.intent.RECEIVE"/>
      
              </intent-filter>
      
          </service>
      
          <service android:name=".activities.services.FirebaseIDService">
              <intent-filter>
                  <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
              </intent-filter>
          </service>
      

      现在在您的设备上运行您的应用程序,然后使用 fire base 推送通知,确保您的代码已正确集成,然后运行 ​​app: 并转到 fire base 云消息传递:

      根据您的应用将数据放入照片中:关闭时:

      如果您的应用正在使用中,那么您的预先写入数据选项将显示,其有关您的同一帐户的促销应用的数据,请不要在此处使用其他帐户应用,

      确保您的密钥应与上述类中的 onMessagede 在 MyFirebaseMessagingService 类中收到的一样

      喜欢

      标题、消息、应用链接、图片网址

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-09-02
        • 1970-01-01
        • 2017-05-01
        • 2019-03-26
        • 1970-01-01
        • 2020-07-23
        相关资源
        最近更新 更多