android - 手动启动 Firebase 服务

标签 android firebase service firebase-cloud-messaging

我正在创建一个包含 FireBase 消息服务的应用程序。
我在 list 中添加了服务:

 <service android:name="xxx.xxx.MyFirebaseInstanceIdService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

 <service android:name="xxx.xxx.MyFireBaseMessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>

我有两个用于获取 token 和 firebase 消息的类:

public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        String recent_token = FirebaseInstanceId.getInstance().getToken();
        SharedPreferences sp = getSharedPreferences("mypref", 0);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("token", recent_token);
        editor.apply();
    }
}

和消息类:

public class MyFireBaseMessagingService extends FirebaseMessagingService {


    @Override
    public void onMessageReceived(final RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        Intent intent = new Intent(MyFireBaseMessagingService.this, AdvCreateActivity.class);
        intent.putExtra("mode","notification");
        intent.putExtra("text",remoteMessage.getNotification().getBody());
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(MyFireBaseMessagingService.this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(MyFireBaseMessagingService.this);
        notificationBuilder.setContentTitle(remoteMessage.getNotification().getTitle());
        notificationBuilder.setContentText(remoteMessage.getNotification().getBody());
        notificationBuilder.setAutoCancel(true);
        notificationBuilder.setSmallIcon(R.mipmap.ic_launcher);
        notificationBuilder.setContentIntent(pendingIntent);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(0,notificationBuilder.build());
    }
}

当应用程序启动时,firebase 服务会自动启动。如果设备没有互联网连接,那么我就无法收到 token 。如果我打开数据或 wifi,firebase 将不会发送 token !
我的问题是:如何在应用程序启动后手动启动 firebase 服务? (我不希望 firebase 服务在启动应用程序时启动!!!)

最佳答案

请在任何您想获取您的 fcm id 的地方调用以下方法。

try {
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();
        Log.d("Firbase id login", "Refreshed token: " + refreshedToken);
    } catch (Exception e) {
        e.printStackTrace();
    }

关于android - 手动启动 Firebase 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53801812/

相关文章:

java - 我想创建一个多用户登录我的 Android 应用程序

swift - testfields 用户输入未进入 firebase 数据库

ios - 我传递的变量不会仅获取视频用户 ID 和个人资料图像 (Swift 3)

spring - Controller、Service、Repository 和简单的委托(delegate)方法

java - 错误 : java. lang.UnsatisfiedLinkError with roboelectric and realm

Android 应用程序图标显示为默认 Android 图标,而不是我的可绘制文件夹中的图标

facebook - symfony 2 - Facebook Bundle - FacebookProvider - 如何在 FacebookProvider 中使用服务和实体管理器?

service - systemctl enable 有效,但 systemctl --user enable 无效

java - 在Android上使用jxl修改excel电子表格

android - 实现滚动以选择项目 ListView