2017-10-10 40 views
-1

我有一個安卓視頻應用,現在我想每當用戶在我的服務器上添加一個新視頻時向每個用戶發送通知,這裏是最新的視頻API從我的服務器[http://flazzin.com//api.php?latest][1] [1]:http://flazzin.com//api.php?latest 我有一個cPanel訪問,但不知道如何發送推送通知給每個用戶,當我上傳一個新的視頻在服務器上。請引導我。我已經探索過Firebase和One Signal,但無法理解如何將這些視頻因素與他們整合,因爲他們有我自己的服務器。安卓視頻應用:當我向服務器添加新視頻時向每個應用用戶發送通知

回答

0

跟着this url正確整合FCM。在用戶使用應用程序時,將fcm註冊令牌保存在服務器數據庫中。

調用下面的功能,同時增加了新的視頻

public function sendTaskNotification(){ 
    $registrationIds = get_all_fcm_tokes(); // Select all saved fcm registration tokens 
    if(count($registrationIds) > 0){ 
     $fcmApiKey = 'YOUR FCM API Key'; 
     $url = 'https://fcm.googleapis.com/fcm/send';//Google URL 

     $message = "New video availiable";//Message which you want to send 
     $title = 'Title'; 

     // prepare the bundle 
     $msg = array('body' => $message,'title' => $title, 'sound' => 'default'); 
     $fields = array('registration_ids' => $registrationIds,'data' => $msg); 

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

     $ch = curl_init(); 
     curl_setopt($ch,CURLOPT_URL, $url); 
     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); 
     if ($result === FALSE) { 
      die('Curl failed: ' . curl_error($ch)); 
     } 
     curl_close($ch);  
     return $result; 
    } 
} 

這個類添加到您的Android項目

public class MyFirebaseMessagingService extends FirebaseMessagingService { 
    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     super.onMessageReceived(remoteMessage); 
     String message = remoteMessage.getData().get("body"); 
     String title = remoteMessage.getData().get("title"); 
     NotificationCompat.Builder builder = 
       new NotificationCompat.Builder(this) 
         .setSmallIcon(R.mipmap.ic_launcher) 
         .setContentTitle(title) 
         .setSound(Uri.parse("android.resource://com.deirki.ffm/" + R.raw.ffmsound)) 
         .setContentText(message); 

     Intent notificationIntent = new Intent(this, MainActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 
       PendingIntent.FLAG_UPDATE_CURRENT); 
     builder.setContentIntent(contentIntent); 

     // Add as notification 
     NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     manager.notify(0, builder.build()); 
    } 

} 

添加上述服務體現

 <service android:name=".MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
      </intent-filter> 
     </service> 
相關問題