android视频应用:当我向服务器添加新视频时,向每个应用用户发送通知

时间:2017-10-10 06:55:48

标签: android api video push-notification server

我有一个Android视频应用程序,现在我想在我的服务器上添加新视频时向每个用户发送通知,这是来自我的服务器的最新视频API [http://flazzin.com//api.php?latest][1] [1]:{{ 3}} 我有一个cPanel访问权限,但我不知道如何在服务器上传新视频时向每个用户发送推送通知。请指导我。我已经探讨过Firebase和One Signal,但无法理解我如何将视频因子与它们集成在一起,因为我认为它们有自己的服务器。

1 个答案:

答案 0 :(得分:0)

关注this网址以正确整合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>