手机睡眠时更新SQLite数据库

时间:2017-03-20 18:44:34

标签: android sqlite firebase firebase-cloud-messaging

我正在尝试更新从Firebase接收通知的SQLite数据库。当手机处于唤醒状态时(屏幕锁定或解锁),它可以正常工作。

当手机处于睡眠状态时,手机会收到通知,但数据库不会更新。

如何更新我的数据库(我真的不想使用Firebase数据库)?

public class MyFcmListenerService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {

        Long lUserId = Long.parseLong(values.get("from_user"));
        Long lPopId = Long.parseLong(values.get("msg_id"));
        String lText = values.get("text");

        Intent service = new Intent(this, DatabaseUpdateService.class);
        service.putExtra("user", lUserId);
        service.putExtra("msg_id", lPopId);
        service.putExtra("text", lText);

        sendNotification(remoteMessage.getNotification().getTitle(), remoteMessage.getNotification().getBody(), service);
    }

    private void sendNotification(String mMessageTitle, String messageBody,  Intent service) {
        Intent intent = new Intent(this, ShowConversationActivity.class);

        Bundle lExtra = service.getExtras();
        intent.putExtras(lExtra);
        intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,PendingIntent.FLAG_ONE_SHOT);

        String lTitle = getResources().getString(R.string.message_from) + " " + mMessageTitle;

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        android.support.v4.app.NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.brand_icon)
                .setContentTitle(lTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(0, notificationBuilder.build());

        // Start the service, keeping the device awake while it is launching.
        Log.d("DatabaseUpdateReceiver", "Starting service @ " + SystemClock.elapsedRealtime());
        startWakefulService(this, service);
    }
}

public class DatabaseUpdateService extends IntentService {
    private long mUserId;
    private Intent mIntent;

    public DatabaseUpdateService() {
        super("DatabaseUpdateService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {

        mIntent = intent;

        ConversationAdapter lConversationAdapter = new ConversationAdapter();

        Bundle lExtras = intent.getExtras();
        long lPopId = lExtras.getLong("user");
        mUserId = lExtras.getLong("msg_id");
        String lText = lExtras.getString("text");

        Log.d(getClass().getName(), "--------------- > " + lText);
        lConversationAdapter.insertMessage(lPopId, mUserId, lText, (long) 0);
    }
}

public ConversationAdapter() {
    mDatabase = DataBaseOpenHelper.getInstance().getDatabase();
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

1:检查您是否提供了WAKE_LOCK权限

2:ConversationAdapter构造函数看起来很奇怪,没有要创建的上下文?