在我的Android应用中,我通过Firebase云消息接收推送通知。通知工作完全正常,唯一的问题是我不希望在应用程序运行时收到通知。当用户使用ap时,我已经在内部显示通知,因此推送通知是多余的并且令人讨厌。以下是我如何调用推送通知的代码:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
NotificationCompat.Builder notificationBuilder = (NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle(messageTitle)
.setContentText(messageBody)
.setAutoCancel(true)
.setVibrate(pattern)
.setLights(Color.BLUE,1,1)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, notificationBuilder.build());
}
我在this link上尝试了解决方案,但在我的情况下它不起作用,因为我确实想要在应用程序处于后台或被杀时发出通知。在应用启动时将布尔值设置为true
,关闭应用时设置false
似乎不是理想的解决方案。必须有一个更好的方法,但我找不到它。任何帮助将不胜感激。
答案 0 :(得分:2)
我所做的是在Application类中使用ActivityLifeCycleCallbacks
public void getdata(String cities) {
String url = "http://api.openweathermap.org/data/2.5/weather?q=" + cities + "&APPID=8072ed7ede0fc9bcb8591a2a2eb90cfd";
StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
CityWeather cityWeather = new CityWeather();
JSONObject object = new JSONObject(response);
cityWeather.setLonitude(String.valueOf(object.getJSONObject("coord").getDouble("lon")));
cityWeather.setLatitude(String.valueOf(object.getJSONObject("coord").getDouble("lat")));
cityWeather.setCityName(object.getString("name"));
cityWeather.setCurrentTemp(String.valueOf(object.getJSONObject("main").getDouble("temp")));
cityWeather.setPressure(String.valueOf(object.getJSONObject("main").getInt("pressure")));
cityWeather.setHumidity(String.valueOf(object.getJSONObject("main").getInt("humidity")));
cityWeather.setMinTemp(String.valueOf(object.getJSONObject("main").getDouble("temp_min")));
cityWeather.setMaxTemp(String.valueOf(object.getJSONObject("main").getDouble("temp_max")));
cityWeather.setWindSpeed(String.valueOf(object.getJSONObject("wind").getDouble("speed")));
cityWeather.setWindDegree(String.valueOf(object.getJSONObject("wind").getInt("deg")));
list.add(cityWeather);
} catch (JSONException e) {
Toast.makeText(MainActivity.this, e.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
}
现在,您可以在 onMessageReceived 中查看。
class MyApplication:Application(){
companion object {
private var activitiesOpen = 0
var displayActivity:String?= null
private set
fun isAppOpen() = activitiesOpen > 0
}
private val activitiesListener = object : ActivityLifecycleCallbacks{
override fun onActivityPaused(activity: Activity?) {
--activitiesOpen
displayActivity = null
}
override fun onActivityResumed(activity: Activity?) {
activitiesOpen++
displayActivity = if(activity != null)
activity::class.simpleName else null
}
override fun onActivityStarted(activity: Activity?) {
}
override fun onActivityDestroyed(activity: Activity?) {
}
override fun onActivitySaveInstanceState(activity: Activity?, outState: Bundle?) {
}
override fun onActivityStopped(activity: Activity?) {
}
override fun onActivityCreated(activity: Activity?, savedInstanceState: Bundle?) {
}
}
}
代码在kotlin中,我希望你对此没有任何问题。