跟着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>