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>