2017-07-11 133 views
-1

我在我的數據庫中上傳視頻併發送通知,但通知無法通過移動設備接收。成功發送通知但未從Firebase接收安卓通知

代碼:

$header = array(
    'header' => array(
    'Authorization'=>'key='.'AAAABxjSQes:APA91bHy_qL4s0BGkiGyCq57fXYtZxzWpfTzRsHZ9AnULieeP1nScb_vuAIYtXob7LEGvvrwBdue5g24iSGDTJGWUG6YcPnAqVx76EKdb5C2P3kl2mF5SrRDAvPWFv0Uqu-HmmVc4wsH', 
    'Content-Type'=>'application/json' 
) 
); 
$http = new HttpSocket(); 
$results = $http->post('https://fcm.googleapis.com/fcm/send', '{ 
    "to" : "cvhXVxTGLl8:APA91bG_iaWHfcka48YwkQPN1kDSIDTk1UAzc-QpHPpnhOPvKBruxnTC_Mxo49EJ3ih2pd9_kGIygs6J_cqMP2ZvoK2I9QVILZH97H8ovsCIuKtDFIr-4jKmBxZJ6E6SeuI_5T3XbfHz", 
    "priority" : "high", 
    "data": { 
    "message": "This is a Firebase Cloud Messaging Topic Message!", 
    } 

}', $header); 

輸出:

{"multicast_id":5769969831923920749,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1499773517218412%4e07cf6cf9fd7ecd"}]} 
[context] => Array 
    (
    ) 

) 

回答

1

結帳火力地堡接收代碼 檢查服務類收到通知,並在清單文件中定義它

Mainfest

<service android:name="CustomFirebaseMessagingService"> 
 
       <intent-filter> 
 
        <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
 
        <action android:name=".SplashActivity" /> 
 
       </intent-filter> 
 
      </service>

檢查服務類CustomFirebaseMessagingService.class

public class CustomFirebaseMessagingService extends FirebaseMessagingService { 
 

 
     private static final String TAG = CustomFirebaseMessagingService.class.getSimpleName(); 
 
     private NotificationManager mNotificationManager; 
 
     private int notificationID = 100; 
 
     private int numMessages = 0; 
 
     String title, message; 
 
     @Override 
 
     public void onMessageReceived(RemoteMessage remoteMessage) { 
 
      Log.d(TAG, "From: " + remoteMessage.getFrom()); 
 

 
      if (remoteMessage.getData().size() > 0) { 
 
       Log.d(TAG, "Message data payload: " + remoteMessage.getData()); 
 
       title = remoteMessage.getData().get("title"); 
 
       message = remoteMessage.getData().get("body"); 
 
      } 
 

 
      // Check if message contains a notification payload. 
 
      if (remoteMessage.getNotification() != null) { 
 
       Log.d(TAG, "Message Notification Body: " + remoteMessage.getNotification().getBody()); 
 
       title = remoteMessage.getNotification().getTitle(); 
 
       message = remoteMessage.getNotification().getBody(); 
 
      } 
 
      
 
\t \t \t //create custom notification code 
 
      displayNotification(getApplicationContext(),title,message); 
 

 
     } 
 

 
     protected void displayNotification(Context context, String PUSH_TITLE, String PUSH_MSG) { 
 
      Log.i("Start", "notification"); 
 

 
      Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher); 
 

 
    \t  /* Invoking the default notification service */ 
 
      NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 
 
      mBuilder.setDefaults(Notification.DEFAULT_ALL); 
 
      mBuilder.setContentTitle(PUSH_TITLE); 
 
      mBuilder.setContentText(PUSH_MSG); 
 
      mBuilder.setTicker("Message from Promo Caribbean"); 
 
      mBuilder.setSmallIcon(R.drawable.ic_top_notification); 
 
      mBuilder.setLargeIcon(icon); 
 

 
    \t  \t /* Increase notification number every time a new notification arrives */ 
 
      mBuilder.setNumber(++numMessages); 
 
\t \t \t NotificationCompat.BigTextStyle notiStyle = new 
 
         NotificationCompat.BigTextStyle(); 
 
       //notiStyle.setBigContentTitle(PUSH_TITLE); 
 
       notiStyle.bigText(PUSH_MSG); 
 
       mBuilder.setStyle(notiStyle);    
 
    \t  \t /* Creates an explicit intent for an Activity in your app */ 
 
      Intent resultIntent = new Intent(context, MainActivity.class); 
 

 
      TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
 
      stackBuilder.addParentStack(SplashActivity.class); 
 

 
    \t  \t /* Adds the Intent that starts the Activity to the top of the stack */ 
 
      stackBuilder.addNextIntent(resultIntent); 
 
      PendingIntent resultPendingIntent = 
 
        stackBuilder.getPendingIntent(
 
          0, 
 
          PendingIntent.FLAG_UPDATE_CURRENT 
 
        ); 
 

 
      mBuilder.setContentIntent(resultPendingIntent); 
 
      mBuilder.setAutoCancel(true); 
 
      mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
 

 
    \t  /* notificationID allows you to update the notification later on. */ 
 
      mNotificationManager.notify(notificationID, mBuilder.build()); 
 
     } 
 
    }

+0

先生我使用PHP – hasnain

+0

通知接收碼是從機器人側不從PHP。 –