0

我已經完全按照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,請別人幫我這裏沒有發現任何邏輯問題,一些不尋常的事情正在發生,而整合推動。

+0

加服去之前,不得不派出由谷歌控制檯測試推如果沒有,則點擊鏈接 - > https://console.firebase.google.com 然後選擇您的項目。選擇您的項目後,點擊導航抽屜列表左側的通知,併發送測試推送鏈接 記得在發送測試推送您的設備應該在後臺或關閉.. – yash786

+0

嘗試使用工具 - > Firebase - >雲消息它可以幫助你找到缺失的論點 –

+0

這些所有的事情我已經嘗試過,但仍然沒有成功。 –

回答

0
public class MyFirebaseMessagingService extends FirebaseMessagingService { 

    private static final String TAG = MyFirebaseMessagingService.class.getSimpleName(); 

    @Override 
    public void onMessageReceived(RemoteMessage remoteMessage) { 
     Log.e(TAG, "From: " + remoteMessage.getFrom()); 


     // Check if message contains a data payload. 
     if (remoteMessage.getData().size() > 0) { 
      Log.e(TAG, "Message data: " + remoteMessage.getData().toString()); 



     } 

     if (remoteMessage.getNotification() != null) { 
      Log.e(TAG, "Message data: " + remoteMessage.getData().toString()); 
      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 notificationsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); 

     NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .setContentTitle("Emoji Keyboard") 
       .setDefaults(-1) 
       .setContentText(body) 
       .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }) 
       .setSound(notificationsound) 
       .setAutoCancel(true) 
       .setContentIntent(pendingIntent); 

     NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(0,notificationBuilder.build()); 
    } 


} 

並在此之後在艙單補充一點:

<!-- Firebase Notifications --> 
     <service android:name=".services.MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT" /> 
      </intent-filter> 
     </service> 

     <service android:name=".services.MyFirebaseInstanceIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT" /> 
      </intent-filter> 
     </service> 
     <!-- ./Firebase Notifications --> 
0

您已經成功獲得令牌後,請確保您已經正確發送到您的服務器。

嘗試recommended action爲無效登記令牌:

檢查登記的格式令牌傳遞給服務器。確保它與客戶端應用程序從註冊Firebase通知時收到的註冊令牌相匹配。不要截斷或添加其他字符。

欲瞭解更多信息,請參閱此documentation

0

代碼在我的應用程序工作正常

添加googleservices-JSON文件從火力創建應用文件夾,並 清單中

<service 
      android:name=".MyFirebaseMessagingService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.MESSAGING_EVENT"/> 
      </intent-filter> 
     </service> 
     <service 
      android:name=".MyFirebaseInstanceIDService"> 
      <intent-filter> 
       <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/> 
      </intent-filter> 
     </service>