【问题标题】:android Video App: send notification to every app user when i add new video to serverandroid Video App:当我将新视频添加到服务器时,向每个应用用户发送通知
【发布时间】:2017-10-10 06:55:48
【问题描述】:

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

【问题讨论】:

    标签: android api video push-notification server


    【解决方案1】:

    关注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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 2015-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多