【发布时间】:2017-01-19 17:16:39
【问题描述】:
我在我的应用中使用 Firebase 通知。一切都设置好了,但是当我从控制台推送通知时,我收到了这个错误:
这是我的代码: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();
}
}
我已在清单中包含所需的服务。
`
【问题讨论】:
-
你把
google-services.json文件放到app里了吗? -
是的,我已经添加了
-
发布您的清单代码
-
发布你的毕业作品
-
一天后它工作了,谢谢:)
标签: java android firebase firebase-notifications