java - Android 通知发送,但未收到(和 onNewToken 不工作)
问题描述
我正在使用 FCM 通知要接收的另一台设备,不知何故我的日志已经到达“发送”,但在另一台设备上没有收到。
这是我活动中的代码:
private void sendNotification(String token) {
Data data = new Data ("Pengajuan Cuti/Ijin","Testing");
NotificationSender sender = new NotificationSender (data, token);
APIService apiService = Client.getClient ("https://fcm.googleapis.com/").create (APIService.class);
apiService.sendNotifcation (sender).enqueue (new Callback<MyResponse> () {
@Override
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
if (response.code() == 200) {
assert response.body () != null;
if (response.body().success != 1) {
Log.d (TAG,"Failed to send");
} else {
Log.d (TAG,"Notification send!");
}
}
}
@Override
public void onFailure(Call<MyResponse> call, Throwable t) {
Log.d (TAG,t.toString ());
}
});
}
这是我的 APIService :
public interface APIService {
@Headers(
{
"Content-Type:application/json",
"Authorization:key=mykey" // Your server key refer to video for finding your server key
}
)
@POST("fcm/send")
Call<MyResponse> sendNotifcation(@Body NotificationSender body);
}
这是我的 MyFirebaseMessagingService :
class MyFireBaseMessagingService extends FirebaseMessagingService {
String title, message;
public String TAG = "mylog";
String CHANNEL_ID = "my_notification";
SessionManager session;
String kodeImei,IPADDR,NMSERVER, URL;
String tag_json_obj = "json_obj_req";
private static final String TAG_SUCCESS = "success";
@Override
public void onNewToken(String token) {
Log.d(TAG, "Refreshed token: " + token);
session = new SessionManager(getApplicationContext());
sendRegistrationToServer(token);
HashMap<String, String> identifyServer = session.getSettingDetails();
IPADDR = String.valueOf(identifyServer.get(SessionManager.KEY_IP));
NMSERVER = String.valueOf(identifyServer.get(SessionManager.KEY_SERVER));
URL = "http://" + IPADDR + "/" + NMSERVER + "/";
HashMap<String, String> devId = session.getUserDetails();
kodeImei = devId.get(SessionManager.KEY_IMEI);
insertToken(kodeImei, token);
}
private void sendRegistrationToServer(String token) {
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("server/saving-data/IDs");
// then store your token ID
ref.push().setValue (token);
}
void insertToken(String kodeImei, String token) {
String url = "http://"+IPADDR+"/"+NMSERVER+"/AttendanceAPI/"+"newtoken.php";
ProgressDialog progressDialog = new ProgressDialog (getApplicationContext ());
progressDialog.setTitle ("Processing");
progressDialog.setMessage ("Please Wait. . .");
progressDialog.setCancelable (true);
progressDialog.show ();
StringRequest strReq = new StringRequest (Request.Method.POST, url, response ->{
Log.d("imei :",kodeImei);
Log.d("token :", token);
try{
JSONObject jObj = new JSONObject (response);
int success = jObj.getInt (TAG_SUCCESS);
Log.d("String Request :",response);
progressDialog.dismiss ();
if(success == 1){
Log.d("Token : ",jObj.getString ("token"));
Log.d(TAG,jObj.getString ("message"));
}else{
Log.d(TAG,jObj.getString ("message"));
}
} catch (Exception e) {
progressDialog.dismiss ();
e.printStackTrace ();
Log.d ("Error :", String.valueOf (e));
}
},error -> {
progressDialog.dismiss ();
Log.e(TAG, "Error: " + error.getMessage());
} ){
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<> ();
params.put("imei", kodeImei);
params.put("token", String.valueOf (token));
return params;
}
};
AppController.getInstance ().addToRequestQueue (strReq, tag_json_obj);
}
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
title = remoteMessage.getData().get("Title");
message = remoteMessage.getData().get("Message");
notification();
}
void notification() {
createNotificationChannel();
Intent intent = new Intent (this, SplashScreen.class);
Log.e ("Title : ", title);
Log.e ("Message : ", message);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_profile)
.setContentTitle(title)
.setContentText(message)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(404, builder.build());
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "my notification";
String description = "general notifications";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
}
}
}
我的问题 :
- 当我运行我的应用程序时,它成功并显示了发送日志,但是在接收设备中不知何故没有新的通知。为什么我做错了?
- onNewToken 不会在应用启动或卸载并再次安装时触发。我不得不强制将令牌获取到我自己的数据库
解决方案
推荐阅读
- linux - Ubuntu:将原始无缓冲数据通过管道传输到 TCP 端口
- php - 有没有办法根据 Symfony 上的条件添加表单字段?
- java - JTextArea:仅限数字
- r - 将正则表达式与 DNA 重叠匹配
- python - 如何使用熊猫数据框获取最大值和相应的列名
- python - GNU Radio Companion:没有名为 iio 的模块;import iio 显示正常
- javascript - 如何根据下拉过滤器动态呈现布局?
- python - Python重塑数组
- javascript - 从函数调用时未呈现 Html
- python - 将多个 CSV 文件(数据集)组合成一个联合文件