【问题标题】:Android - Firebase push notification not workingAndroid - Firebase 推送通知不起作用
【发布时间】:2017-09-01 11:08:54
【问题描述】:

我已经完全按照 Google 教程所述创建和设置 Firebase 控制台。我也在我的项目中实现了服务。当我从 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 天,请有人帮助我,因为这里没有发现逻辑问题,集成推送时发生了一些不寻常的事情。

【问题讨论】:

  • 在去之前必须从谷歌控制台发送测试推送,如果没有然后点击链接 --> console.firebase.google.com 然后选择你的项目。选择您的项目后,然后单击导航抽屉列表左侧的通知并发送测试推送链接记住发送测试推送时您的设备应该处于后台或关闭..
  • 尝试使用工具 -> Firebase -> Cloud Messaging 它可以帮助您找到缺失的参数
  • 这些我都试过了,但还是没有成功。
  • 所以refreshedToken 收到了吗?

标签: android firebase google-cloud-messaging firebase-cloud-messaging android-push-notification


【解决方案1】:
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 -->

【讨论】:

    【解决方案2】:

    成功获取令牌后,请确保已将其正确发送到服务器。

    尝试recommended action 获取无效的注册令牌:

    检查您传递给服务器的注册令牌的格式。确保它与客户端应用通过 Firebase 通知注册收到的注册令牌相匹配。不要截断或添加其他字符。

    有关详细信息,请参阅此documentation

    【讨论】:

      【解决方案3】:

      代码在我的应用程序中运行良好

      在从 firebase 创建的 app 文件夹中添加 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>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-04-29
        • 1970-01-01
        • 2020-06-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多