在Firebase消息传递系统中,onMessageReceived()
方法在应用程序关闭时运行并获得推送通知。因此,我想在onMessageReceived()
方法中使用SQLite。 SQLite必须需要一个上下文,但是由于应用程序已关闭,所以无法在该方法中获取ApplicationContext。关闭应用程序后如何获取应用程序上下文?
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
DatabaseHelper database = new DatabaseHelper(FirebaseMessagingService.this);
database.updateTicket(Integer.parseInt(remoteMessage.getData().get("idx")));
try{
Badges.setBadge(this, 1);
}catch(BadgesNotSupportedException e){
e.printStackTrace();
}
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
String channelId = "Default";
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(remoteMessage.getNotification().getTitle())
.setContentText(remoteMessage.getNotification().getBody()).setAutoCancel(true).setContentIntent(pendingIntent);;
NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
manager.createNotificationChannel(channel);
}
manager.notify(0, builder.build());
//sendPushNotification(remoteMessage.getData().get("title"), remoteMessage.getData().get("body"));
}
这是我的onMessageReceived代码。问题是前两行不起作用的数据库代码。我已经检查过它可以在应用程序上运行,也可以在后台运行。
答案 0 :(得分:1)
服务具有自己的上下文,并且为FirebaseMessagingService
extends Service
因此您可以使用<?php
$_config = $this->helper('Sm\Market\Helper\Data');
$headerStyle = $_config->getThemeLayout('header_style');
$_mobile_detect = $this->_objectManager-
>get('Sm\Market\Helper\MobileDetect');
$deviceType = ($_mobile_detect->isMobile() ? ($_mobile_detect->isTablet()
? 'tablet' : 'phone') : 'computer');
//$deviceType = ($_mobile_detect->isMobile() && $_mobile_detect-
>isTablet() == false) ? 'phone' : 'computer';
//var_dump($deviceType); die('here');
if ( $deviceType == 'computer') {
include($block->getTemplateFile("Magento_Theme::html/header-style/" .
$headerStyle . ".phtml"));
}else{
include($block->getTemplateFile("Magento_Theme::html/header-style/header-
10.phtml"));
}
?>
或this