【问题标题】:android notification firebase安卓通知火力基地
【发布时间】:2017-12-06 06:01:56
【问题描述】:

我通过PHP向android发送了一个通知,在android上就正常了。

但是当我发送多个时,它只显示最后一个,我希望它显示所有。

有可能吗?

我该怎么做?

PHP

$msg = array
          (
        'nome'  => $comando,
          );
    $fields = array
            (
                'to'    => $token,
                'data'  => $msg
            );


    $headers = array
            (   
                'Content-Type: application/json',
                'Authorization: key= KEY'               
            );

#Send Reponse To FireBase Server    
        $ch = curl_init();
        curl_setopt( $ch,CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send' );
        curl_setopt( $ch,CURLOPT_POST, true );
        curl_setopt( $ch,CURLOPT_HTTPHEADER, $headers );
        curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
        curl_setopt( $ch,CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt( $ch,CURLOPT_POSTFIELDS, json_encode( $fields ) );
        $result = curl_exec($ch );
        curl_close( $ch );

#Echo Result Of FireBase Server
echo $result;

安卓

Intent intent = new Intent(this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410,
                        intent, PendingIntent.FLAG_UPDATE_CURRENT);

                Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
                alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                NotificationCompat.Builder notificationBuilder = new
                        NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setSound(alert)
                        .setContentTitle("teste")
                        .setContentText("teste "+msg+"!")
                        .setAutoCancel(true)
                        .setOnlyAlertOnce(true)
                        .setPriority(2)
                        .setContentIntent(pendingIntent);

                NotificationManager notificationManager =
                        (NotificationManager)
                                getSystemService(Context.NOTIFICATION_SERVICE);

                notificationManager.notify(1410, notificationBuilder.build());

【问题讨论】:

    标签: php android firebase android-notifications


    【解决方案1】:

    很简单:

    notificationManager.notify(1410, notificationBuilder.build());
    

    你已经写了 '1410',而不是每次都分配 Unique_Integer_number。

    每次都独一无二:

    Random random = new Random();
    int m = random.nextInt(9999 - 1000) + 1000;
    

    【讨论】:

      【解决方案2】:

      从此行中删除 1410 并同时更改 PendingIntent 行。

      PendingIntent pendingIntent = PendingIntent.getActivity(this, 1410,
                          intent, PendingIntent.FLAG_UPDATE_CURRENT);
      notificationManager.notify(1410, notificationBuilder.build());
      

      添加此行以生成唯一 id 并使用它来通知。

      int id = (int) System.currentTimeMillis();
      PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                      notificationIntent, 0);
      notificationManager.notify(id, notificationBuilder.build());
      

      【讨论】:

        【解决方案3】:

        在通知新通知之前取消所有通知..

        NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.cancelAll();
        
        notificationManager.notify(1410, notificationBuilder.build());
        

        【讨论】:

          【解决方案4】:

          问题出在这一行

           notificationManager.notify(1410, notificationBuilder.build());
          

          每个通知的第一个参数 id 应该不同。否则它将覆盖前一个。

          一个简单的方法是使用 SharedPreference 来获取和存储 Id 。并且每次使用一个递增的。请参阅下面的示例。

           private int getNotificationId(){
              SharedPreferences  sp = PreferenceManager.getDefaultSharedPreferences(this);
              int lastId=sp.getInt("notify_id",0);
              sp.edit().putInt("notify_id",lastId+1).commit();
              return lastId;
          }
          
           notificationManager.notify(getNotificationId(), notificationBuilder.build());
          

          【讨论】:

            【解决方案5】:

            在 android 代码中,您只使用一个通知 id,将其更改为动态,您将看到所有通知。

            更改以下行:

            notificationManager.notify(getUniqueId(), notificationBuilder.build());
            
            ...
            private int getUniqueId() {
                return (int) (System.currentTimeMillis() / 1000);
            }
            

            【讨论】:

              【解决方案6】:

              您使用了PendingIntent.FLAG_UPDATE_CURRENT,并且每个Notification 都没有唯一的ID。用它来显示Notification -

              private void sendNotification(String messageBody) {
                      int uniqueNotificationId = (int) (System.currentTimeMillis() & 0xfffffff);
                      Intent intent = new Intent(this, LoginActivity.class); // Where you will go after clicking on notification 
                      intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                      PendingIntent pendingIntent = PendingIntent.getActivity(this, uniqueNotificationId /* Request code */, intent,
                              PendingIntent.FLAG_ONE_SHOT);
              
                     //PendingIntent pendingIntent= PendingIntent.getActivity(this, uniqueNotificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
              
              
                      Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                      android.support.v4.app.NotificationCompat.Builder notificationBuilder =
                              new NotificationCompat.Builder(this)
                                      .setSmallIcon(R.mipmap.ic_launcher)
                                      .setContentTitle("FCM Message")
                                      .setContentText(messageBody)
                                      .setAutoCancel(true)
                                      .setSound(defaultSoundUri)
                                      .setContentIntent(pendingIntent);
              
                      NotificationManager notificationManager =
                              (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
              
                      notificationManager.notify(uniqueNotificationId  /* ID of notification */, notificationBuilder.build());
                  }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2016-09-17
                • 2020-10-20
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多