2017-09-01 131 views
0

我正在實現firebase雲消息傳遞,以在滿足錶行數據條件時從mysql數據庫接收通知。我現在擁有的只是一個簡單的推送通知,當我使用瀏覽器輸入一個php文件。以下是我的所有編碼。當條件設置時,Android fcm接收來自mysql的通知

push_notification.php

<?php 

function send_notification ($tokens, $message) 
{ 
    $url = 'https://fcm.googleapis.com/fcm/send'; 

    $fields = array(
     'registration_ids' => $tokens, 
     'data' => $message 
     ); 

    $headers = array(
     'Authorization:key = AAAA0********* ', 
     '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_VERIFYHOST, 0); 
    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; 
} 

$conn = mysqli_connect("localhost", "root", "", "fcm"); 
$sql = " Select fcm_token From fcm_notification"; 
$result = mysqli_query($conn, $sql); 
$tokens = array(); 

if(mysqli_num_rows($result) > 0){ 
    while ($row = mysqli_fetch_assoc($result)) { 
     $tokens[] = $row["fcm_token"]; 
    } 
} 

mysqli_close($conn); 
$message = array("message" => " FCM PUSH NOTIFICATION TEST MESSAGE"); 
$message_status = send_notification($tokens, $message); 
echo $message_status; 

?> 

MyFirebaseInstanceIDService.java

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { 

@Override 
public void onTokenRefresh() { 

    String token = FirebaseInstanceId.getInstance().getToken(); 

    registerToken(token); 
} 

private void registerToken(String token) { 

    OkHttpClient client = new OkHttpClient(); 
    RequestBody body = new FormBody.Builder() 
      .add("fcm_token", token) 
      .build(); 

    Request request = new Request.Builder() 
      .url("http://192.168.1.5/fcm/register.php") 
      .post(body) 
      .build(); 

    try { 
     client.newCall(request).execute(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

} 

MyFirebaseMessagingService.java

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 

    showNotification(remoteMessage.getData().get("message")); 
} 

private void showNotification(String message) { 

    Intent i = new Intent(this,MainActivity.class); 
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this) 
      .setAutoCancel(true) 
      .setContentTitle("FCM Test") 
      .setContentText(message) 
      .setLights(Color.BLUE,1,1) 
      .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark) 
      .setContentIntent(pendingIntent); 

    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    manager.notify(0,builder.build()); 
} 
} 

fcm_token表

token

眼下我如何能夠接收來自MySQL數據庫的通知,當用戶balance小於0.5,這是我user

user table

回答

0

您已經建立了足夠的系統來完成發送和接收推送通知的基本功能。

從我得到的是,你有一個系統,當用戶餘額小於0.5時,你想發送推送通知。您可以使用PHP編寫代碼,其中有平衡操作來檢查用戶的當前餘額併發送推送通知。

OR

您可以設置一個cron檢查每個用戶的平衡,並將它們發送通知,如果有餘額小於0.5

+0

我聽說過cron作業,但我不知道如何使用它,也許我不打算使用它。我如何在PHP中編寫代碼?我從來沒有這樣做過,你能告訴我第一步嗎? – lolol