我已經完全按照Google教程中的提示創建並設置了Firebase控制檯。我也在我的項目中實施了服務。雖然我從Firebase控制檯發送郵件,但未在我的應用中收到。當我嘗試使用單個設備發送時,它顯示「未註冊的註冊令牌」。Android - Firebase推送通知不起作用
這裏是我的MyFirebaseInstanceIDService:
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
@Override
public void onTokenRefresh() {
//Getting registration token
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
System.out.println("TOKEN::" + refreshedToken);
//Displaying token on logcat
SmartApplication.REF_SMART_APPLICATION.writeSharedPreferences("regId", refreshedToken);
// SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
// SharedPreferences.Editor editor = pref.edit();
// editor.putString("regId", refreshedToken);
// editor.commit();
}
}
這裏是我的MyFirebaseMessagingService:
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFirebaseMsgService";
private static int count = 0;
String TYPE = "type";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
System.out.println("data getting");
Log.d(TAG, "Notification Message TITLE: " + remoteMessage.getNotification().getTitle());
Log.d(TAG, "Notification Message BODY: " + remoteMessage.getNotification().getBody());
Log.d(TAG, "Notification Message DATA: " + remoteMessage.getData().toString());
//Calling method to generate notification
//remoteMessage.getNotification().getBody()
sendNotification(remoteMessage.getNotification().getTitle(),
remoteMessage.getNotification().getBody(), remoteMessage.getData());
}
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String messageTitle, String messageBody, Map<String, String> row) {
PendingIntent contentIntent = null;
try {
Intent groupDetailIntent = new Intent(this, UnanimousHomeActivity.class);
contentIntent = PendingIntent.getActivity(this, (int) (Math.random() * 100),
groupDetailIntent, PendingIntent.FLAG_UPDATE_CURRENT);
} catch (Exception e) {
e.printStackTrace();
}
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(contentIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(count, notificationBuilder.build());
count++;
}
}
由於圖4天,我已經stucked,請別人幫我這裏沒有發現任何邏輯問題,一些不尋常的事情正在發生,而整合推動。
加服去之前,不得不派出由谷歌控制檯測試推如果沒有,則點擊鏈接 - > https://console.firebase.google.com 然後選擇您的項目。選擇您的項目後,點擊導航抽屜列表左側的通知,併發送測試推送鏈接 記得在發送測試推送您的設備應該在後臺或關閉.. – yash786
嘗試使用工具 - > Firebase - >雲消息它可以幫助你找到缺失的論點 –
這些所有的事情我已經嘗試過,但仍然沒有成功。 –