我正在使用FCM服务将通知推送到我的应用程序。但是,当我为我的应用程序进行一些功能测试以确保一切正常通知时,设备真实的或模拟器停止接收任何更多通知,尽管我得到“成功”发送通知。这意味着我发送~5通知并与他们互动,如果我再发送,我不再能收到任何新的通知。 所以我的问题是:GetNotification函数(下面是否)可能导致此问题?还是来自Android的系统?
[我甚至注意到,有时如果我关闭wifi并再次打开,我会收到应该发送到设备的丢失通知(我100%确定我在接收任何通知时已连接到互联网) ]
发送通知代码:
public void SendNotification(final Context context, String requestToken, final String serverKey, String fcmUrl, final Requests request)
{
String msg = "msg";
String title = "title";
JSONObject obj = null;
JSONObject objData = null;
try {
obj = new JSONObject();
objData = new JSONObject();
objData.put("body", msg);
objData.put("title", title);
obj.put("to", requestToken);
obj.put("notification", objData);
} catch (JSONException e)
{
e.printStackTrace();
}
JsonObjectRequest jsObjRequest =
new JsonObjectRequest(Request.Method.POST,fcmUrl, obj,
new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
Util.makeToast(context,"Success");
}
},
new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Util.makeToast(context,"Fail");
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError
{
Map<String, String> params = new HashMap<>();
params.put("Authorization", "key=" + serverKey);
params.put("Content-Type", "application/json");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(context);
int socketTimeout = 1000 * 60;// 60 seconds
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
jsObjRequest.setRetryPolicy(policy);
requestQueue.add(jsObjRequest);
}
这将在我的服务类中创建一个实现FirebaseMessagingService的通知:
private void GetNotification(String messageTitle, String messageBody)
{
Intent intent = new Intent(this, MainController.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("serve","PLEASE");
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 , intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(messageTitle)
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}