অ্যান্ড্রয়েড বিজ্ঞপ্তি শব্দ


152

আমি নতুন নোটিফিকেশন কমপ্যাট বিল্ডার ব্যবহার করেছি এবং আমি কোনও শব্দ করার জন্য বিজ্ঞপ্তিটি পেতে পারি না। এটি কম্পন এবং আলো ফ্ল্যাশ করবে। অ্যান্ড্রয়েড ডকুমেন্টেশন একটি স্টাইল সেট করতে বলে যা আমি এটি করেছিলাম:

builder.setStyle(new NotificationCompat.InboxStyle());

কিন্তু কোন শব্দ নেই?

সম্পূর্ণ কোড:

NotificationCompat.Builder builder =  
        new NotificationCompat.Builder(this)  
        .setSmallIcon(R.drawable.ic_launcher)  
        .setContentTitle("Notifications Example")  
        .setContentText("This is a test notification");  


Intent notificationIntent = new Intent(this, MenuScreen.class);  

PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent,   
        PendingIntent.FLAG_UPDATE_CURRENT);  

builder.setContentIntent(contentIntent);  
builder.setAutoCancel(true);
builder.setLights(Color.BLUE, 500, 500);
long[] pattern = {500,500,500,500,500,500,500,500,500};
builder.setVibrate(pattern);
builder.setStyle(new NotificationCompat.InboxStyle());
// Add as notification  
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
manager.notify(1, builder.build());  

12
বিল্ডার.সেটসাউন্ড (সেটিংস.সিসটেম.ডিএএফএএল.এল.এফ.এইফ.আল.অন্টিফিকেশন_উইআরআই) এরও কাজ করা উচিত
জার ই আহমর

উত্তর:


256

আমার আগের কোড থেকে কী অনুপস্থিত ছিল:

Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
builder.setSound(alarmSound);

4
এটি খেলতে থাকে এবং থামবে না, আমি কীভাবে এটি একবারে বেজে উঠব? builder.setOnlyOnce (সত্য) ব্যবহার করে; সাহায্য করবে না
সালিস

এটির এক সময় খেলা
ব্ল্যাকহক

2
builder.setOnlyOnce (সত্য) আমার ক্ষেত্রে আমার সমস্যা সমাধান করেছে!
এলওজকার

155

কেবলমাত্র আপনার শব্দ ফাইলটি Res\raw\siren.mp3ফোল্ডারে রাখুন, তারপরে এই কোডটি ব্যবহার করুন:

কাস্টম সাউন্ডের জন্য:

Notification notification = builder.build();
notification.sound = Uri.parse("android.resource://"
            + context.getPackageName() + "/" + R.raw.siren);

ডিফল্ট সাউন্ডের জন্য:

notification.defaults |= Notification.DEFAULT_SOUND;

কাস্টম ভাইব্রেটের জন্য:

long[] vibrate = { 0, 100, 200, 300 };
notification.vibrate = vibrate;

ডিফল্ট ভাইব্রেটের জন্য:

notification.defaults |= Notification.DEFAULT_VIBRATE;

52

ডিফল্ট শব্দের জন্য অন্য উপায়

builder.setDefaults(Notification.DEFAULT_SOUND);

12

কোডিং কোড ব্যবহার করতে পারেন

 String en_alert, th_alert, en_title, th_title, id;
 int noti_all, noti_1, noti_2, noti_3, noti_4 = 0, Langage;

 class method
 Intent intent = new Intent(context, ReserveStatusActivity.class);
 PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


 intent = new Intent(String.valueOf(PushActivity.class));
 intent.putExtra("message", MESSAGE);
 TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
 stackBuilder.addParentStack(PushActivity.class);
 stackBuilder.addNextIntent(intent);
 // PendingIntent pendingIntent =
 stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

 //      android.support.v4.app.NotificationCompat.BigTextStyle bigStyle = new NotificationCompat.BigTextStyle();
 //        bigStyle.bigText((CharSequence) context);



 notification = new NotificationCompat.Builder(context)
    .setSmallIcon(R.mipmap.ic_launcher)
    .setContentTitle(th_title)
    .setContentText(th_alert)
    .setAutoCancel(true)

 // .setStyle(new Notification.BigTextStyle().bigText(th_alert)  ตัวเก่า
 //

 .setStyle(new NotificationCompat.BigTextStyle().bigText(th_title))

    .setStyle(new NotificationCompat.BigTextStyle().bigText(th_alert))

    .setContentIntent(pendingIntent)
    .setNumber(++numMessages)


    .build();

 notification.sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

 notificationManager.notify(1000, notification);

10

কেবল নীচের সহজ কোডটি রাখুন:

notification.sound = Uri.parse("android.resource://"
        + context.getPackageName() + "/" + R.raw.sound_file);

ডিফল্ট সাউন্ডের জন্য:

notification.defaults |= Notification.DEFAULT_SOUND;

8

আপনাকে রিংটোনম্যানেজার ব্যবহার করতে হবে

private static final int MY_NOTIFICATION_ID = 1;
    private NotificationManager notificationManager;
    private Notification myNotification;

    private final String myBlog = "http://niravranpara.blogspot.com/";

অ্যালার্ম রিংটোন সহ নোফিকেশন ম্যানেজারের জন্য কোড আপনি রিং টোন রিংটোনম্যানেজার সেট করতে পারেন

notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri
                        .parse(myBlog));
                  PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                    Notification note = new Notification(R.drawable.ic_launcher, "Alarm", System.currentTimeMillis());
                    note.setLatestEventInfo(getApplicationContext(), "Alarm", "sound" + " (alarm)", pi);
                    Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
                    if(alarmSound == null){
                        alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
                        if(alarmSound == null){
                            alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                        }
                    }
                    note.sound = alarmSound;
                    note.defaults |= Notification.DEFAULT_VIBRATE;
                    note.flags |= Notification.FLAG_AUTO_CANCEL;
                    notificationManager.notify(MY_NOTIFICATION_ID, note);

দুঃখিত, আপনি এটি কীভাবে নতুন নোটিফিকেশনকম্প্যাট.বিল্ডারের জন্য করেন তা নয়।
জেমস এমভি 20

বিজ্ঞপ্তিটি নোটিফিকেশনকম্প্যাট.বিল্ডার.ইউবিল্ড () ফাংশন দিয়ে তৈরি করা যায় এবং বিল্ড () এর রিটার্ন মান গ্রহণ করা এবং নোটিফিকেশন ম্যানেজ.নোটাইফাইয়ে পাস করার আগে এর মানগুলি সংশোধন করা ঠিক আছে। এটি বেশি অর্থবহ নয়, তবে এটি পুরোপুরি ঠিক।
হলগ্যাক

6

আপনাকে বিল্ডার ব্যবহার করতে হবে। setSound

Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);  

                PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent,   
                        PendingIntent.FLAG_UPDATE_CURRENT);  

                builder.setContentIntent(contentIntent);  
                builder.setAutoCancel(true);
                builder.setLights(Color.BLUE, 500, 500);
                long[] pattern = {500,500,500,500,500,500,500,500,500};
                builder.setVibrate(pattern);
                builder.setStyle(new NotificationCompat.InboxStyle());
                 Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
                    if(alarmSound == null){
                        alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
                        if(alarmSound == null){
                            alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
                        }
                    }

                // Add as notification  
                NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);  
             builder.setSound(alarmSound);
                manager.notify(1, builder.build());  

1
আপনি দুবার রিংটোনম্যানেজ.টায়িপ্লিআরিংটোন লিখেছেন।
বার্নার্ডো ফেরারি

6

আপনি একটি ফাংশন করতে পারেন:

public void playNotificationSound() 
{
    try
    {

        Uri alarmSound = `Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + MyApplication.getInstance().getApplicationContext().getPackageName() + "/raw/notification");`
        Ringtone r = RingtoneManager.getRingtone(MyApplication.getInstance().getApplicationContext(), alarmSound);
        r.play();
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
}

আপনি বিজ্ঞপ্তি পেলে এই ফাংশনটিতে কল করুন।

এখানে কাঁচাটি হল রেজোল্ডে ফোল্ডার এবং বিজ্ঞপ্তিটি কাঁচা ফোল্ডারে থাকা শব্দ ফাইল।


6

ওরিওতে (অ্যান্ড্রয়েড 8) এবং তার উপরে এটি কাস্টম সাউন্ডের জন্য এইভাবে করা উচিত (বিজ্ঞপ্তি চ্যানেল):

Uri soundUri = Uri.parse(
                         "android.resource://" + 
                         getApplicationContext().getPackageName() +
                         "/" + 
                         R.raw.push_sound_file);

AudioAttributes audioAttributes = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_ALARM)
            .build();

// Creating Channel
NotificationChannel channel = new NotificationChannel("YOUR_CHANNEL_ID",
                                                      "YOUR_CHANNEL_NAME",
                                                      NotificationManager.IMPORTANCE_HIGH);
channel.setSound(soundUri, audioAttributes);

((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE))
                                           .createNotificationChannel(notificationChannel);

5

1 ম কাঁচা ফোল্ডারে "yourmp3file" .mp3 ফাইল রাখুন (অর্থাত্ Res ফোল্ডারের অভ্যন্তরে)

আপনার কোড পুতে দ্বিতীয় ..

Notification noti = new Notification.Builder(this)
.setSound(Uri.parse("android.resource://" + v.getContext().getPackageName() + "/" + R.raw.yourmp3file))//*see note

আমি আমার অনক্লিকের ভিতরে এটিই রেখেছি (ভি ভি) কেবলমাত্র "প্রসঙ্গ ()। GetPackageName ()" হিসাবে সেখান থেকে কাজ করবে না কারণ এটি কোনও প্রসঙ্গ পাবেন না


4

অ্যান্ড্রয়েড ওরিও বা পরবর্তী সংস্করণে সিস্টেমের সাথে চ্যানেলটি নিবন্ধন করুন; আপনি একই চ্যানেলের (অ্যাপটি আনইনস্টল করার আগে) এর পরে গুরুত্ব বা অন্যান্য বিজ্ঞপ্তি আচরণগুলি পরিবর্তন করতে পারবেন নাএখানে চিত্র বর্ণনা লিখুন

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

channel.setSound(Settings.System.DEFAULT_NOTIFICATION_URI,audioAttributes);

অগ্রাধিকার এছাড়াও সর্বাধিক গুরুত্বপূর্ণ এখানে ব্যবহার করে বিজ্ঞপ্তি অগ্রাধিকার সেট উচ্চ

ব্যবহারকারী-দৃশ্যমান গুরুত্ব স্তরের গুরুত্ব (অ্যান্ড্রয়েড 8.0 এবং উচ্চতর)

1) জরুরী একটি হেডস-আপ বিজ্ঞপ্তি হিসাবে একটি শব্দ এবং প্রদর্শিত হয় তোলে -> IMPORTANCE_HIGH
2) উচ্চ একটি শব্দ তোলে -> IMPORTANCE_DEFAULT
3) মাঝারি কোনো শব্দ -> IMPORTANCE_LOW
4) নিম্ন কোন শব্দ এবং স্থিতি দন্ডে বলে মনে হচ্ছে না -> ইমপোর্টট্যান্স_মিন

একই ক্রমে অগ্রাধিকার হিসাবে কাজ করে (অ্যান্ড্রয়েড 7.1 এবং নিম্ন)

1) প্রাইরিটি_হাইট বা প্রাইরিটি_ম্যাক্স

2) PRIORITY_DEFAULT

3) PRIORITY_LOW

4) PRIORITY_MIN


1
" আপনি একই চ্যানেলের পরে গুরুত্ব বা অন্যান্য বিজ্ঞপ্তি আচরণগুলি পরিবর্তন করতে পারবেন না "। কাজ করার জন্য অ্যাপটিকে আনইনস্টল করা দরকার, ফলস্বরূপ এই ক্রিয়াটি ডিভাইস থেকে চ্যানেলের তথ্য মোছা।
এলি ড্যান্টাস


1
private void showNotification() {

    // intent triggered, you can add other intent for other actions
    Intent i = new Intent(this, MainActivity.class);
    PendingIntent pIntent = PendingIntent.getActivity(this, 0, i, 0);

    //Notification sound
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // this is it, we'll build the notification!
    // in the addAction method, if you don't want any icon, just set the first param to 0
    Notification mNotification = null;
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) {

        mNotification = new Notification.Builder(this)

           .setContentTitle("Wings-Traccar!")
           .setContentText("You are punched-in for more than 10hrs!")
           .setSmallIcon(R.drawable.wingslogo)
           .setContentIntent(pIntent)
           .setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 })
           .addAction(R.drawable.favicon, "Goto App", pIntent)
           .build();

    }

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    // If you want to hide the notification after it was selected, do the code below
    // myNotification.flags |= Notification.FLAG_AUTO_CANCEL;

    notificationManager.notify(0, mNotification);
}

আপনি যেখানেই চান এই ফাংশন কল করুন। এটি আমার জন্য কাজ করেছে


0

বিজ্ঞপ্তি দ্বারা। বিল্ডার শ্রেণীর উদাহরণ (বিল্ডার) যা নীচে দেওয়া হয়েছে আপনি বিজ্ঞপ্তিতে ডিফল্ট শব্দটি খেলতে পারেন:

builder.setDefaults(Notification.DEFAULT_SOUND);

0
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user);

    btn= findViewById(R.id.btn); 

   btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            notification();
        }
    });
  }   

ব্যক্তিগত শূন্য বিজ্ঞপ্তি () {

    NotificationCompat.Builder builder= new NotificationCompat.Builder(this);
    builder.setAutoCancel(true);
    builder.setContentTitle("Work Progress");
    builder.setContentText("Submit your today's work progress");
    builder.setSmallIcon(R.drawable.ic_email_black_24dp);
    Intent intent=new Intent(this, WorkStatus.class);
    PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, intent, 
    PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setContentIntent(pendingIntent);
    builder.setDefaults(Notification.DEFAULT_VIBRATE);
    builder.setDefaults(Notification.DEFAULT_SOUND);

    NotificationManager notificationManager= (NotificationManager) 
    getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());
}

এটি শব্দ এবং কম্পন সহ সম্পূর্ণ বিজ্ঞপ্তি


0

নির্মাতা বা বিজ্ঞপ্তির উপর নির্ভর করবেন না। ভাইব্রেটের জন্য কাস্টম কোড ব্যবহার করুন।

public static void vibrate(Context context, int millis){
    try {
        Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            v.vibrate(VibrationEffect.createOneShot(millis, VibrationEffect.DEFAULT_AMPLITUDE));
        } else {
            v.vibrate(millis);
        }
    }catch(Exception ex){
    }
}

-1

আপনি নিম্নলিখিতটি করতে পারেন:

MediaPlayer mp;
mp =MediaPlayer.create(Activity_Order_Visor_Atender.this, R.raw.ok);         
mp.start();

আপনি আপনার সংস্থানগুলির মধ্যে কাঁচা নাম সহ একটি প্যাকেজ তৈরি করেন এবং সেখানে আপনি আপনার শব্দ রাখেন তারপরে আপনি কেবল এটি কল করেন।


-1

// সেট নোটিফিকেশন অডিও (অ্যান্ড্রয়েড 10 পর্যন্ত পরীক্ষিত)

builder.setDefaults(Notification.DEFAULT_VIBRATE);
//OR 
builder.setDefaults(Notification.DEFAULT_SOUND);
আমাদের সাইট ব্যবহার করে, আপনি স্বীকার করেছেন যে আপনি আমাদের কুকি নীতি এবং গোপনীয়তা নীতিটি পড়েছেন এবং বুঝতে পেরেছেন ।
Licensed under cc by-sa 3.0 with attribution required.