2016-12-13 5 views
0

mySQL DBのテーブルに格納されている最初のトークンとして設定されている1つのデバイスでのみ通知を受け取り、通知は残りのトークン番号に送信されません。私はWHILEループを試して、配列にトークン番号を格納しましたが、動作しませんでした。FCMを使用して複数のデバイスに通知を送信できません

解決策をご提案ください。ありがとうございました。ここで

私のコードです:

enter image description here

<?php 
require "init.php"; 
$message=$_POST['message']; 
$title=$_POST['title']; 
$path_to_fcm='https://fcm.googleapis.com/fcm/send'; 
$server_key="A*************************Q"; 
$sql="select token from fcm_info"; 
$result =mysqli_query($con,$sql); 
$row=mysqli_fetch_row($result); 
$key=$row[0]; 
$headers = array(

    'Authorization:key=' .$server_key, 
    'Content-Type:application/json' 
); 
$fields =array('to'=>$key, 
       'notification'=>array('title'=>$title,'body'=>$message)); 

$payload =json_encode($fields); 
$curl_session =curl_init(); 
curl_setopt($curl_session,CURLOPT_URL, $path_to_fcm); 
curl_setopt($curl_session,CURLOPT_POST, true); 
curl_setopt($curl_session,CURLOPT_HTTPHEADER, $headers); 
curl_setopt($curl_session,CURLOPT_RETURNTRANSFER,true); 
curl_setopt($curl_session,CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($curl_session,CURLOPT_IPRESOLVE, CURLOPT_IPRESOLVE); 
curl_setopt($curl_session,CURLOPT_POSTFIELDS, $payload); 
$result=curl_exec($curl_session); 
curl_close($curl_session); 
mysqli_close($con); 
?> 
+2

コードを追加して、スクリーンショットを作成しないでください。 – sinhayash

+0

配列としてsql結果を取得し、それを使用してリクエストを呼び出します –

+0

上記のコードを編集しました。配列を使用しましたが動作しませんでした。通知を1つのデバイスに送信しました。コードはまったく初心者ですから。 – KuNal

答えて

0

あなたがメソッドのロジックを送信する通知をカバーし、各イテレーションのメソッドは、メソッドにトークンとメッセージを渡すことをループ&通話を開始する必要があります。

+0

こんにちは、私は前にループしていたので、ループを開始し、繰り返しを行う場所を説明してください。しかし、私の投稿を編集したので、コードは利用可能です。 – KuNal

2

「to」の代わりに「registration_ids」を使用し、FCMでマルチキャストを使用するためにカンマ区切りの複数の登録IDを渡します。最終的なペイロードは次のようにする必要があります:

{ 
"registration_ids":["id1","id2",...], 
    "priority" : "normal", 
    "data" : { 
    "title" : "Title", 
    "message" : "Message to be send", 
    "icon": "icon_path" 
    } 
} 

を私はServicesを使用することをお勧めしたいと思います "ソリューションを提案してください" もっと助け

0

ためhttps://developers.google.com/cloud-messaging/http-server-refを参照してください。 Android Studio hereのドキュメントを読むことをお勧めします。

HelloService

と呼ばれるクラスを作成しますが、そこサービスについて感じることがたくさんですが、私はスニペットは、あなたに最も参考になると信じた瞬間に、ここで少しコードは、

、あなたが自分自身に思うかもしれない「これはoverhwlemingされる」

public class HelloService extends Service { 
private Looper mServiceLooper; 
private ServiceHandler mServiceHandler; 

// Handler that receives messages from the thread 
private final class ServiceHandler extends Handler { 
    public ServiceHandler(Looper looper) { 
     super(looper); 
    } 
    @Override 
    public void handleMessage(Message msg) { 
     // Normally we would do some work here, like download a file. 
     // For our sample, we just sleep for 5 seconds. 
     try { 
      Thread.sleep(5000); 
     } catch (InterruptedException e) { 
      // Restore interrupt status. 
      Thread.currentThread().interrupt(); 
     } 
     // Stop the service using the startId, so that we don't stop 
     // the service in the middle of handling another job 
     stopSelf(msg.arg1); 
    } 
} 

@Override 
public void onCreate() { 
// Start up the thread running the service. Note that we create a 
// separate thread because the service normally runs in the process's 
// main thread, which we don't want to block. We also make it 
// background priority so CPU-intensive work will not disrupt our UI. 
HandlerThread thread = new HandlerThread("ServiceStartArguments", 
     Process.THREAD_PRIORITY_BACKGROUND); 
thread.start(); 

// Get the HandlerThread's Looper and use it for our Handler 
mServiceLooper = thread.getLooper(); 
mServiceHandler = new ServiceHandler(mServiceLooper); 
    } 

    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
    Toast.makeText(this, "servicestarting",Toast.LENGTH_SHORT).show(); 


    Message msg = mServiceHandler.obtainMessage(); 
    msg.arg1 = startId; 
    mServiceHandler.sendMessage(msg); 

    // If we get killed, after returning from here, restart 
    return START_STICKY; 
     } 

@Override 
public IBinder onBind(Intent intent) { 
    // We don't provide binding, so return null 
    return null; 
      } 


@Override 
public void onDestroy() { 
    Toast.makeText(this, "service done", Toast.LENGTH_SHORT).show(); 
} 
} 

*適切な輸入品との内部に次のコードを貼り付けます。しかしそれは逆です。代わりにFirebaseからのメッセージをプッシュするサービス+ Firebase

ため

例、のは、以前のdatabasereferenceを作成し、あなたは変更は、データベースの1

最初に行われたときにユーザに通知したいとしましょう'のhandleMessageメソッド' へのonCreate

mDatabaseLike=FirebaseDatabase.getInstance().getReference().child("Likes"); 

ゴーオンと

次を追加ここ
 @Override 
    public void handleMessage(Message msg) { 



     mDatabaseLike.addValueEventListener(new ValueEventListener() { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) { 

      notifyUserOfDBupdate() 


      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) { 

      } 
     }); 




     //stopSelf(msg.arg1); 
    } 
} 

はnotifyUserOfDBupdate方法とどのように今、あなたの本当のデバイスとエミュレータで二時間に一回、あなたのアプリケーションを実行し、ユーザー

private void notifyUserOfDBupdate() { 
    //Intents 
    Intent Pdf_view = new Intent(this, //class to throw the user when they hit on notification\\.class); 
    PendingIntent pdf_view = PendingIntent.getActivity(this, 0, Pdf_view, 0); 


    //Notification Manager 
    NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE); 


    //The note 
    Uri soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    Notification noti = new NotificationCompat.Builder(getApplicationContext()) 
      .setTicker("TickerTitle") 
      .setContentTitle("content title") 
      .setSound(soundUri) 
      .setContentText("content text") 
      .setContentIntent(pdf_view).getNotification(); 


    //Execution 
    noti.flags = Notification.FLAG_AUTO_CANCEL; 
    nm.notify(0, noti); 
} 

に通知することです。 2つのうちのいずれか1つがfirebaseデータベースを変更すると、もう一方のデータベースには即時に通知されます。

HandleMessageメソッドの中で好きな方法を変更します。それはあなたがそれを殺すことができない限り、永遠になるでしょう。

親切について

関連する問題