2017-10-10 42 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> 
相关问题