Я создаю приложение android для моего фипа. Я выполнил весь свой код, но застрял с уведомлениями firebase pu sh. Есть два разных пользователя этого приложения: один покупатель, а другой - продавец. Я попытался реализовать уведомление pu sh в своем приложении для продавца. В приложении «хранитель магазина», когда он принимает заказ клиента, я хочу отправить уведомление в приложении клиента. Я пытался использовать этот код, но не смог отправить уведомление. Я использую базу данных реального времени firebase для этого приложения.
Последние 3 дня я пытался решить эту проблему, но не смог ее решить. Я также ищу на youtube и стековом потоке, но не могу найти какое-либо удовлетворительное решение.
Код, который я пишу для реализации firebase, упоминается pu sh.
фрагментОрдера
public class fragmentOrders extends Fragment {
private View orderView;
private CompositeDisposable compositeDisposable = new CompositeDisposable();
private IFCMService ifcmService;
private DatabaseReference tokenRef;
public fragmentOrders() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
orderView = inflater.inflate(R.layout.fragment_orders, container, false);
ifcmService = RetrofitFCMClient.getInstance().create(IFCMService.class);
tokenRef = FirebaseDatabase.getInstance().getReference(common.TOKEN_REF);
return orderView;
}
@Override
public void onStop() {
super.onStop();
compositeDisposable.clear();
}
private void showOrders() {
FirebaseRecyclerOptions<OrderModel> options = new FirebaseRecyclerOptions.Builder<OrderModel>()
.setQuery(orderRef,OrderModel.class)
.build();
FirebaseRecyclerAdapter<OrderModel,OrderViewHolder> adapter = new FirebaseRecyclerAdapter<OrderModel, OrderViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull OrderViewHolder holder, int position, @NonNull OrderModel model) {
holder.CustomerName.setText(model.getName());
holder.CustomerNumber.setText(model.getNodePhone());
holder.ShippingAddress.setText(model.getAddress());
holder.OrderDateTime.setText(model.getDate() + " , " + model.getTime());
holder.TotalPrice.setText(model.getTotalprice());
holder.btnConfirmOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
holder.cancelLayout.setVisibility(View.GONE);
holder.btnConfirmOrder.setText(model.getUid());
confirmOrder(model);
}
});
}
@NonNull
@Override
public OrderViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(getContext()).inflate(R.layout.view_order_items,parent,false);
return new OrderViewHolder(view);
}
};
orderList.setAdapter(adapter);
adapter.startListening();
}
}
});
}
private void confirmOrder(OrderModel model) {
tokenRef.child(model.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
TokenModel tokenModel = dataSnapshot.getValue(TokenModel.class);
Map<String,String> notiData = new HashMap<>();
notiData.put(common.NOTI_TITLE,"Order Confirmed");
notiData.put(common.NOTI_CONTENT,"Your Shopping Order is Confirmed and Shipped");
//ShowNotification(tokenModel,notiData);
FCMSendData sendData = new FCMSendData(tokenModel.getToken(),notiData);
compositeDisposable.add(ifcmService.sendNotification(sendData)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<FCMResponse>() {
@Override
public void accept(FCMResponse fcmResponse) throws Exception {
if (fcmResponse.getSuccess() == 1){
Toast.makeText(getContext(), "Order confirmed success", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getContext(), "Order confirmed success but faild to send notification", Toast.LENGTH_SHORT).show();
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
Toast.makeText(getContext(), throwable.getMessage(), Toast.LENGTH_SHORT).show();
}
})
);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public class OrderViewHolder extends RecyclerView.ViewHolder{
TextView CustomerName,CustomerNumber,ShippingAddress,OrderDateTime,TotalPrice;
Button btnViewOrderProducts,btnConfirmOrder,btnCancelOrder,btnCallCustomer;
RelativeLayout cancelLayout;
public OrderViewHolder(@NonNull View itemView) {
super(itemView);
CustomerName = itemView.findViewById(R.id.customer_name);
CustomerNumber = itemView.findViewById(R.id.customer_phone);
ShippingAddress = itemView.findViewById(R.id.customer_shipping_address);
OrderDateTime = itemView.findViewById(R.id.order_date_time);
btnViewOrderProducts = itemView.findViewById(R.id.btn_view_order_product);
TotalPrice = itemView.findViewById(R.id.order_total_price);
btnConfirmOrder = itemView.findViewById(R.id.btn_confirm_order);
btnCancelOrder = itemView.findViewById(R.id.btn_cancel_order);
btnCallCustomer = itemView.findViewById(R.id.btn_call_customer);
cancelLayout = itemView.findViewById(R.id.canel_layout);
}
}
}
общий
public class common {
public static final String NOTI_TITLE = "title";
public static final String NOTI_CONTENT = "content";
public static final String TOKEN_REF = "Tokens";
public static void showNotification(Context context, int id, String title, String content, Intent intent) {
PendingIntent pendingIntent = null;
if (intent !=null){
pendingIntent = PendingIntent.getActivity(context,id,intent,PendingIntent.FLAG_UPDATE_CURRENT);
String NOTIFICATION_CHANNEL_ID = "Designer_Club_App";
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID
, "Designer Club", NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("Designer Club");
notificationChannel.enableLights(true);
notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
notificationChannel.enableVibration(true);
notificationChannel.setLightColor(android.R.color.white);
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(title)
.setContentText(content)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
if (pendingIntent != null){
builder.setContentIntent(pendingIntent);
Notification notification = builder.build();
notificationManager.notify(id,notification);
}
}
}
}
FCMResponse
import java.util.List;
public class FCMResponse {
private long multicast_id;
private int success,failure,canonical_ids;
private List<FCMResult> results;
private long message_id;
public FCMResponse() {
}
public long getMulticast_id() {
return multicast_id;
}
public void setMulticast_id(long multicast_id) {
this.multicast_id = multicast_id;
}
public int getSuccess() {
return success;
}
public void setSuccess(int success) {
this.success = success;
}
public int getFailure() {
return failure;
}
public void setFailure(int failure) {
this.failure = failure;
}
public int getCanonical_ids() {
return canonical_ids;
}
public void setCanonical_ids(int canonical_ids) {
this.canonical_ids = canonical_ids;
}
public List<FCMResult> getResults() {
return results;
}
public void setResults(List<FCMResult> results) {
this.results = results;
}
public long getMessage_id() {
return message_id;
}
public void setMessage_id(long message_id) {
this.message_id = message_id;
}
}
FCMResult
public class FCMResult {
private String message_id;
public FCMResult() {
}
public String getMessage_id() {
return message_id;
}
public void setMessage_id(String message_id) {
this.message_id = message_id;
}
}
FCMSendData
import java.util.Map;
public class FCMSendData {
private String to;
private Map<String,String> data;
public FCMSendData(String to, Map<String, String> data) {
this.to = to;
this.data = data;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public Map<String, String> getData() {
return data;
}
public void setData(Map<String, String> data) {
this.data = data;
}
}
IFCMService
import io.reactivex.Observable;
import retrofit2.http.Body;
import retrofit2.http.Headers;
import retrofit2.http.POST;
public interface IFCMService {
@Headers({
"Content-Type:application/json",
"Authorization:key=AAAAzkiH50E:APA91bETfnykxzAHx7Q4g5EFcPqZRHuyWi8S72OlnzFvxY4x_9o5MWl-BuxSB2ZUYt73Im1RJsOb55d7cTe3IPZR4fLIAaoJd_CaH1THCq3lxV4Rs6DfDdTtrC2HgIKNEgON2rD2ICGb"
})
@POST("fcm/send")
Observable<FCMResponse> sendNotification(@Body FCMSendData body);
}
RetrofitFCMClient
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitFCMClient {
private static Retrofit instance;
public static Retrofit getInstance(){
if (instance == null)
instance = new Retrofit.Builder()
.baseUrl("https://fcm.googleapis.com/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
return instance;
}
}
MyFCMService
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
public class MyFCMService extends FirebaseMessagingService {
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Map<String,String> dataRecv = remoteMessage.getData();
if (dataRecv != null){
common.showNotification(this,new Random().nextInt(),
dataRecv.get(common.NOTI_TITLE),
dataRecv.get(common.NOTI_CONTENT),
null);
}
}
@Override
public void onNewToken(String s) {
super.onNewToken(s);
common.updateToken(this,s);
}
}
зависимости
implementation 'com.google.firebase:firebase-auth:19.3.0'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-messaging:20.1.4'
implementation 'com.google.firebase:firebase-storage:19.1.1'
implementation 'com.firebaseui:firebase-ui-database:5.0.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.6.1'
implementation 'com.squareup.retrofit2:converter-gson:2.6.1'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxjava:2.2.9'
манифест
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.designerclubadmin">
<application
<service android:name=".services.MyFCMService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
</application>
</manifest>