'发送消息时出错' Firebase云消息传递服务

时间:2016-09-12 09:39:26

标签: java android firebase firebase-notifications

我在我的应用中使用了Firebase通知。一切都已设置但是当我从控制台推送通知时,我收到此错误:

Screenshot

这是我的代码:MyFirebaseInstanceIDService.class

package notes.ronak.com.pushnotificationfromstart;

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;

/**
 * Created by ronaksakhuja on 12/9/16.
 */
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
    private static final String TAG="MyFirebaseInsIDServicve";

    @Override
    public void onTokenRefresh() {
        //Get updated token
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG,"New Token : "+refreshedToken);
        sendRegistrationToServer(refreshedToken);


        //You can save the token into third party server to do anything you want
    }

    private void sendRegistrationToServer(String refreshedToken) {
    }
}

MyFirebaseMessagingService.class

package notes.ronak.com.pushnotificationfromstart;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;

/**
 * Created by ronaksakhuja on 12/9/16.
 */
public class MyFirebaseMessagingService extends FirebaseMessagingService {
    private static final String TAG="MyFirebaseMsgService";

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Log.d(TAG,"FROM :"+remoteMessage.getFrom());

        //check if msg contains data

        if(remoteMessage.getData().size()>0){
            Log.d(TAG,"Message data : "+remoteMessage.getData());
        }

        //check if msg contains notification

        if(remoteMessage.getNotification()!=null){
            Log.d(TAG,"Message body : "+remoteMessage.getNotification().getBody());
            sendNotification(remoteMessage.getNotification().getBody());
        }

    }

    private void sendNotification(String body) {
        Intent intent = new Intent(this,MainActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
        Uri notiifcationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notification = new NotificationCompat.Builder(this).setContentTitle("Firebase Title").setContentText(body).setContentIntent(pendingIntent).setSound(notiifcationSound).setAutoCancel(true);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notification.build());
    }
}

MainActivity.class

package notes.ronak.com.pushnotificationfromstart;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.google.firebase.iid.FirebaseInstanceId;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG="MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button showtoken= (Button) findViewById(R.id.button);
        showtoken.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        //Get the token
        String token = FirebaseInstanceId.getInstance().getToken();
        Log.d(TAG,"Token : "+token);
        Toast.makeText(MainActivity.this, "Token : "+token, Toast.LENGTH_SHORT).show();

    }
}

我已在清单中包含所需的服务。

`

1 个答案:

答案 0 :(得分:0)

只需清除您的浏览器缓存。

退出Firebase控制台并再次登录。

更改您的浏览器。