如何使用Firebase将推送通知从服务器发送到应用程序?

问题描述:

我试图使用Firebase将推送通知从服务器发送到应用,但是当通知到达应用时,我的应用崩溃了。

PHP代码:
如何使用Firebase将推送通知从服务器发送到应用程序?

 <?php 

     $registrationIds = array("USER-DEVICE-TOKEN"); 

     $message = array 
     (
     'message' => 'My awesome message', 
     'title'  => 'My awesome title', 
     'subtitle' => 'My awesome subtitle', 
     'tickerText' => 'My awesome Ticker text', 
     'vibrate' => 1, 
     'sound'  => 1, 
     'largeIcon' => 'large_icon', 
     'smallIcon' => 'small_icon' 
     ); 


     $url = 'https://fcm.googleapis.com/fcm/send'; 
     $fields = array(
     'registration_ids' => $registrationIds, 
     'data' => $message 
     ); 
     $headers = array(
     'Authorization:key = GOOGLE-API-KEY', 
     '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); 

     echo $result; 

Output of PHP Script

我的火力地堡通讯服务:

public class MyFirebaseMessagingService extends FirebaseMessagingService { 

private static final String TAG = "MyFirebaseMsgService"; 

@Override 
public void onMessageReceived(RemoteMessage remoteMessage) { 
    //Displaying data in log 
    //It is optional 
    Log.d(TAG, "From: " + remoteMessage.getFrom()); 
    Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody()); // LINE 24 
    //Calling method to generate notification 
    sendNotification(remoteMessage.getNotification().getBody(),remoteMessage.getNotification().getTitle()); 
} 

//This method is only generating push notification 
//It is same as we did in earlier posts 
private void sendNotification(String messageBody,String messageTitle) { 
    Intent intent = new Intent(this, LoginActivity.class); 
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 
      PendingIntent.FLAG_ONE_SHOT); 

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
      .setSmallIcon(R.drawable.mainlogo) 
      .setContentTitle(messageTitle) 
      .setContentText(messageBody) 
      .setAutoCancel(true) 
      .setSound(defaultSoundUri) 
      .setContentIntent(pendingIntent); 

    NotificationManager notificationManager = 
      (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    notificationManager.notify(0, notificationBuilder.build()); 
} 
     } 

Crashing error when push notification comes

希望对你有所帮助

+1

NPE位于文件MyFirebaseMessagingService的第24行。这个错误是相当具有描述性的。在那里放置一个断点并检查发生了什么。 –

+0

你确实有好英语没有任何错误 – MatejMecka

+0

@bigdestroyer准备好了,我用注释标记了行“// LINE 24” –

检查remoteMessage.getNotification()是否不为NULL。我认为它是NULL,因为你不在你的PHP代码的$fields数组中发送参数“通知”。你正在发送'数据'。因此,当您发送参数“数据”时,您必须使用remoteMessage.getData()获取该参数。

尝试在onMessageReceived方法中打印remoteMessage.getData().toString()的内容。它应该打印通知中发送的数据。

希望这会有所帮助。

+0

感谢您的帮助,我已将“数据”更改为“我的php数组中的”通知“,它的工作原理! –

+0

@YuraRosiak我写了相同的代码,但它不工作。你能帮忙吗? –

+0

@TashenJazbi您能否提供有关您获得的错误的详细信息?我的意思是,你是否收到系统托盘中的通知?你有没有得到onMessageReceived调用?你有什么异常? – GeorgeLBA