Java 发送消息时,用户未收到推送通知。应用程序没有';不要崩溃,也可以';不要说我哪里出了错。只有其他用户不知道';我不明白
在我的应用程序中,当用户之间发送消息时,其他用户应收到推送通知。我看了一个教程来学习如何做到这一点,一年前的许多课程现在都被弃用了。无论如何,应用程序不会崩溃,但当用户发送消息时,不会显示任何通知 有人能告诉我我错过了什么吗?我可以看到,Java 发送消息时,用户未收到推送通知。应用程序没有';不要崩溃,也可以';不要说我哪里出了错。只有其他用户不知道';我不明白,java,android,push-notification,firebase-cloud-messaging,Java,Android,Push Notification,Firebase Cloud Messaging,在我的应用程序中,当用户之间发送消息时,其他用户应收到推送通知。我看了一个教程来学习如何做到这一点,一年前的许多课程现在都被弃用了。无论如何,应用程序不会崩溃,但当用户发送消息时,不会显示任何通知 有人能告诉我我错过了什么吗?我可以看到,Tokens节点已在Firebase中创建,Tokens>UserId>Url,但正如我所说,其他用户没有收到推送通知 有人能指出我收到Toast消息的原因吗?每当有人发送消息时,唯一发生的事情就是我收到了Toast消息。没有别的事情发生。为什么Toast消息会
Tokens
节点已在Firebase中创建,Tokens>UserId>Url
,但正如我所说,其他用户没有收到推送通知
有人能指出我收到Toast
消息的原因吗?每当有人发送消息时,唯一发生的事情就是我收到了Toast
消息。没有别的事情发生。为什么Toast
消息会响
MyFirebaseMessaging
public class MyFirebaseMessaging extends FirebaseMessagingService {
String NOTIFICATION_CHANNEL_ID = "com.e.events";
@Override
public void onMessageReceived(@NonNull RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
String sent = remoteMessage.getData().get("sent");
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
if (firebaseUser != null && sent.equals(firebaseUser.getUid())) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
sendOreoNotification(remoteMessage);
} else {
sendNotification(remoteMessage);
}
}
}
private void sendOreoNotification(RemoteMessage remoteMessage) {
String user = remoteMessage.getData().get("user");
String icon = remoteMessage.getData().get("icon");
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RemoteMessage.Notification notification = remoteMessage.getNotification();
int j = Integer.parseInt(user.replaceAll("[\\D]", ""));
Intent intent = new Intent(this, MessageActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userid", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, j, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
OreoNotification oreoNotification = new OreoNotification(this);
Notification.Builder builder = oreoNotification.getOreoNotification(title, body, pendingIntent, defaultSound, icon);
int i = 0;
if (j > 0) {
i = j;
}
oreoNotification.getManager().notify(i, builder.build());
}
private void sendNotification(RemoteMessage remoteMessage) {
String user = remoteMessage.getData().get("user");
String icon = remoteMessage.getData().get("icon");
String title = remoteMessage.getData().get("title");
String body = remoteMessage.getData().get("body");
RemoteMessage.Notification notification = remoteMessage.getNotification();
int j = Integer.parseInt(user.replaceAll("[\\D]", ""));
Intent intent = new Intent(this, MessageActivity.class);
Bundle bundle = new Bundle();
bundle.putString("userid", user);
intent.putExtras(bundle);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, j, intent, PendingIntent.FLAG_ONE_SHOT);
Uri defaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(Integer.parseInt(icon))
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setSound(defaultSound)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
int i = 0;
if (j > 0) {
i = j;
}
notificationManager.notify(i, builder.build());
}
@Override
public void onNewToken(@NonNull String s) {
super.onNewToken(s);
Log.d("TokenFirebase", s);
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
String refreshToken = FirebaseInstanceId.getInstance().getInstanceId().getResult().getToken();
if (firebaseUser != null) {
updateToken(refreshToken);
}
}
private void updateToken(String refreshToken) {
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Tokens");
Token token = new Token(refreshToken);
reference.child(firebaseUser.getUid()).setValue(token);
}
}
消息活动
private void sendNotification(String receiver, String username, String message) {
DatabaseReference tokens = FirebaseDatabase.getInstance().getReference("Tokens");
Query query = tokens.orderByKey().equalTo(receiver);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Token token = snapshot.getValue(Token.class);
Data data = new Data(mFirebaseUser.getUid(), R.mipmap.ic_launcher, username + ": " + message, "New Message", mId);
Sender sender = new Sender(data, token.getToken());
mAPIService.sendNotification(sender).enqueue(new Callback<MyResponse>() {
@Override
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
if (response.code() == 200) {
if (response.body().success != 1) {
Toast.makeText(MessageActivity.this, "Unsuccessful", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<MyResponse> call, Throwable t) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void sendNotification(字符串接收者、字符串用户名、字符串消息){
DatabaseReference令牌=FirebaseDatabase.getInstance().getReference(“令牌”);
Query Query=tokens.orderByKey().equalTo(接收方);
addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
对于(DataSnapshot快照:DataSnapshot.getChildren()){
Token-Token=snapshot.getValue(Token.class);
数据数据=新数据(mFirebaseUser.getUid(),R.mipmap.ic_启动器,用户名+”:“+消息,“新消息”,mId);
发送方=新发送方(数据,token.getToken());
mAPIService.sendNotification(发送方).enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
if(response.code()==200){
if(response.body().success!=1){
Toast.makeText(MessageActivity.this,“Unsuccessful”,Toast.LENGTH_SHORT.show();
}
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
}
});
}
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
});
}
应用程序设置中的显示通知
是否已启用?@user1987所有功能均已启用,只是通知未显示。既然应用程序并没有崩溃,我只是想检查一下所有的东西,看看有什么可能是错误的。你们可能想查看Firebase的响应,success
和failure
查看它们是否包含附加信息,以确保它不是Firebase
或您的请求问题。我认为您应该查看最新教程:Firebase.google.com/docs/cloud-messaging/android/client。至少我看到了服务的错误意图过滤器。@Баааааааааааааа1072。是否已启用应用程序设置中的显示通知
?@user1987所有功能均已启用,只是通知未显示。既然应用程序并没有崩溃,我只是想检查一下所有的东西,看看有什么可能是错误的。你们可能想查看Firebase的响应,success
和failure
查看它们是否包含附加信息,以确保它不是Firebase
或您的请求问题。我认为您应该查看最新教程:Firebase.google.com/docs/cloud-messaging/android/client。至少我看到了服务的错误意图过滤器。@Баааааааааааааа1072。还没起作用