Hunny Arora :
I was trying to make a simple app. But I am getting a simple problem like I am not receiving notification in my device which is Android pie. I make a button in a fragment, and when I click it I make a method to show the notification to the user, but when I click my button I didn't receive any notification. So if anyone finds a problem in my code please answer me. Here is my code below:- Main Fragment:-
public class SearchFragment extends Fragment implements View.OnClickListener {
View view;
private Button clickme;
private NotificationManagerCompat notificationManagerCompat;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_search, container, false);
clickme = view.findViewById(R.id.Click_me);
notificationManagerCompat = NotificationManagerCompat.from(getActivity());
clickme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "simple toast", Toast.LENGTH_SHORT).show();
sendOnChannel1();
}
});
return view;
}
@Override
public void onClick(View view) {
}
public void sendOnChannel1() {
NotificationCompat.Builder mBuilder = (NotificationCompat.Builder)
new NotificationCompat.Builder(getContext())
.setSmallIcon(R.drawable.ic_android_black_24dp)
.setContentTitle("Simple notification")
.setContentText("This is a test");
NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(getContext().NOTIFICATION_SERVICE);
notificationManager.notify(0, mBuilder.build());
}
}
activity fragment.xml:-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="search"
android:textColor="@color/ColorRed"
android:gravity="center"
android:layout_centerInParent="true"
android:textSize="20sp"/>
<Button
android:id="@+id/Click_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me"
/>
</RelativeLayout>
Thanks for reading my question please reply me if anyone finds a problem.
Black mamba :
Try this its working properly
//send notification
private void sendNotification() {
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
assert audio != null;
switch (audio.getRingerMode()) {
case AudioManager.RINGER_MODE_NORMAL:
break;
case AudioManager.RINGER_MODE_SILENT:
break;
case AudioManager.RINGER_MODE_VIBRATE:
break;
}
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_background)
.setAutoCancel(true)
.setGroupSummary(true);
// Since android Oreo notification channel is needed.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel notificationChannel = new NotificationChannel("chanelid", "Notification", importance);
notificationChannel.enableLights(true);
assert notificationManager != null;
notificationBuilder.setChannelId("chanelid");
if (notificationManager != null) {
List<NotificationChannel> channelList = notificationManager.getNotificationChannels();
for (int i = 0; channelList != null && i < channelList.size(); i++) {
notificationManager.deleteNotificationChannel(channelList.get(i).getId());
}
}
notificationManager.createNotificationChannel(notificationChannel);
}
notificationManager.notify(1, notificationBuilder.build());
}
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325564&siteId=1