ハニーアローラ:
私はシンプルなアプリを作るしようとしていました。しかし、私はAndroidのパイは私のデバイスで通知を受信していないような単純な問題を取得しています。私は、フラグメントのボタンを作る、と私はそれをクリックしたときに、私は、ユーザーに通知を表示する方法を作るが、私は私のボタンをクリックしたときに、私はすべての通知を受信しませんでした。誰もが私のコードで問題が見つかった場合ので、私に答えてください。ここでは以下の私のコードです: - 主なフラグメントは: -
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());
}
}
活動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>
誰もが問題を見つけた場合、私の質問を読んでくれてありがとう、私を返信してください。
ブラックマンバ:
適切にこのその作業を試してみてください
//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());
}