Java android的FCM通知(按主题) FCM(按主题)
**一,。问题是,当用户向卖家发送订单时,通知不会出现,IDE也不会出现错误Java android的FCM通知(按主题) FCM(按主题),java,android,push-notification,firebase-cloud-messaging,Java,Android,Push Notification,Firebase Cloud Messaging,**一,。问题是,当用户向卖家发送订单时,通知不会出现,IDE也不会出现错误 问题和解决方案在哪里** 代码 MyFirebaseMessaging的类 代码如下: public class MyFirebaseMessaging extends FirebaseMessagingService { privat e static final String NOTIFICATION_CHANNEL_ID = "MY_NOTIFICATION_CHANNEL
- 问题和解决方案在哪里**
代码
- 代码如下:
public class MyFirebaseMessaging extends FirebaseMessagingService { privat e static final String NOTIFICATION_CHANNEL_ID = "MY_NOTIFICATION_CHANNEL_ID"; //required for andriod 8 and bove private FirebaseAuth firebaseAuth; private FirebaseUser firebaseUser; @Override public void onMessageReceived(@NonNull RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); //all notification will be received here firebaseAuth = FirebaseAuth.getInstance(); firebaseUser = firebaseAuth.getCurrentUser(); //get data from notification String notificationType = remoteMessage.getData().get("notificationType"); if (notificationType.equals("NewOrder")){ String buyerUid = remoteMessage.getData().get("buyerUid"); String sellerUid = remoteMessage.getData().get("sellerUid"); String orderId = remoteMessage.getData().get("orderId"); String notificationTitle = remoteMessage.getData().get("notificationTitle"); String notificationDescription = remoteMessage.getData().get("notificationMessage"); if (firebaseUser != null && firebaseAuth.getUid().equals(sellerUid)){ //user is signed in and same user to which notification is sent showNotification(orderId,sellerUid,buyerUid,notificationTitle ,notificationDescription,notificationType); } } if (notificationType.equals("OrderStatusChanged")){ String buyerUid = remoteMessage.getData().get("buyerUid"); String sellerUid = remoteMessage.getData().get("sellerUid"); String orderId = remoteMessage.getData().get("orderId"); String notificationTitle = remoteMessage.getData().get("notificationTitle"); String notificationDescription = remoteMessage.getData().get("notificationMessage"); if (firebaseUser != null && firebaseAuth.getUid().equals(buyerUid)){ //user is signed in and same user to which notification is sent
}
}
}
private void showNotification(String orderId,String sellerUid,String
buyerUid,String notificationTitle,String notificationDescription,String notificationType){
//notification
NotificationManager notificationManager = (NotificationManager)getSystemService
(Context.NOTIFICATION_SERVICE);
//id for notification ,random
int notificationID = new Random().nextInt(3000);
//check if android version is 8 and above
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
setupNotificationChannel(notificationManager);
}
//handle notification click ,start order activity
Intent intent = null;
if (notificationType.equals("NewOrder")){
//open orderDetailsSellerActivity
intent = new Intent(this, OrderDetailsSellerActivity.class);
intent.putExtra("orderId",orderId);
intent.putExtra("orderBy",buyerUid);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
}else if (notificationType.equals("OrderStatusChanged")){
intent = new Intent(this, OrderDetailsUsersActivity.class);
intent.putExtra("orderId",orderId);
intent.putExtra("orderTo",sellerUid);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
}
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_ONE_SHOT);
//large Icon
Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),R.drawable.shop);
//sound of notification
Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new
NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setSmallIcon(R.drawable.shop)
.setLargeIcon(largeIcon)
.setContentTitle(notificationTitle)
.setContentText(notificationDescription)
.setSound(notificationSoundUri)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
//show notification
notificationManager.notify(notificationID,notificationBuilder.build());
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void setupNotificationChannel(NotificationManager notificationManager) {
CharSequence channelName = "Some Sample Text";
String channelDescription = "Channel Description here";
NotificationChannel notificationChannel = new
NotificationChannel(NOTIFICATION_CHANNEL_ID,channelName,NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setDescription(channelDescription);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.enableVibration(true);
if (notificationManager != null){
notificationManager.createNotificationChannel(notificationChannel);
}
}