在Firebase通知Android中获取数据有效载荷

时间:2018-11-04 00:04:34

标签: php android firebase firebase-cloud-messaging android-volley

我使用php作为后端服务器发送Firebase通知,我通过Volley Library将android连接到PHP。

我希望我的应用程序发送链接作为通知,并且当用户单击该应用程序时,该链接将在Webview中打开。

但是服务器仅在日志中发送标题,而不发送url,它表明URL为null或{}

我该如何解决这个问题。

public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private final String CHANNEL_ID = "notificcation";

@Override
public void onNewToken(String s) {
    super.onNewToken(s);
    Log.e("NEW_TOKEN", s);
    FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
        @Override
        public void onSuccess(InstanceIdResult instanceIdResult) {
            String recent_token = instanceIdResult.getToken();
            // Do whatever you want witiiph your token now
            // i.e. store it on SharedPreferences or DB
            // or directly send it to server
            SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(getString(R.string.FCM_PREF), Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putString(getString(R.string.FCM_TOKEN), recent_token);
            editor.commit();


        }
    });

}




@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    super.onMessageReceived(remoteMessage);
    String title=remoteMessage.getNotification().getTitle();
    String message=remoteMessage.getNotification().getBody();

    Log.d(TAG, "Message data payload: " + remoteMessage.getData());


    Intent intent =new Intent(this,MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntent =PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder builder=new NotificationCompat.Builder(getBaseContext(),CHANNEL_ID);
    builder. setContentTitle(title)
            .setContentText(message)
            .setWhen(System.currentTimeMillis())
            .setDefaults(Notification.DEFAULT_ALL)
            .setSmallIcon(R.drawable.common_google_signin_btn_icon_dark_normal)
         .setContentIntent(pendingIntent)
            .setPriority(NotificationCompat.PRIORITY_HIGH);


    NotificationManager notificationManager = (NotificationManager)getBaseContext().getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1,builder.build());


}

}

0 个答案:

没有答案