bnnoor
bnnoor

Reputation: 696

notification sound not work for api 10 android

I use this function to show status notification. All thing is correct but no sound play on notification.

public void notifiction_main(String ticker,String title,String text,int _icon){

        String ns = mContext.NOTIFICATION_SERVICE;
        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(ns);
        int icon = R.id.icon;
        CharSequence tickerText = ticker; // ticker-text
        long when = System.currentTimeMillis();
        CharSequence contentTitle = title;
        CharSequence contentText = text;
        Intent notificationIntent = new Intent(mContext, MainActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(mContext, 0, notificationIntent, 0);
        Notification notification = new Notification(icon, tickerText, when);
        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.setLatestEventInfo(mContext, contentTitle, contentText,contentIntent);
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        mNotificationManager.notify(1, notification);


    }

I set vibrate after this but vibrate not work too :(

Upvotes: 4

Views: 629

Answers (2)

Kapil Rajput
Kapil Rajput

Reputation: 11545

You should use NotificationCompat.Builder as Notification class is depricated in API level 20 and higher

and use this piece of code i hope it will work for u

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

        //icon appears in device notification bar and right hand corner of notification
        builder.setSmallIcon(R.drawable.notificationg);
        Bitmap icon = BitmapFactory.decodeResource(getApplicationContext().getResources(),
                R.drawable.ic_launcher);
        builder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(icon));
        Intent i = new Intent();
        PendingIntent pIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), i, 0);

        builder.addAction(R.drawable.ic_launcher,"OK",pIntent);
        // This intent is fired when notification is clicked
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

        // Set the intent that will fire when the user taps the notification.
        builder.setContentIntent(pendingIntent);

        // Large icon appears on the left of the notification
        builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));

        // Content title, which appears in large type at the top of the notification
        builder.setContentTitle(notificationTitle);

        // Content text, which appears in smaller text below the title
        builder.setContentText(notificationMessage);

        Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        builder.setSound(alarmSound);
        // The subtext, which appears under the text on newer devices.
        // This will show-up in the devices with Android 4.2 and above only
        builder.setSubText("Tap to go to link in notifications.");

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        // Will display the notification in the notification bar
        notificationManager.notify(NOTIFICATION_COUNT, builder.build());

Notification tone will work fine with Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); builder.setSound(alarmSound);

Upvotes: 2

Pankaj
Pankaj

Reputation: 8058

You can use NotificationCompat.Builder

NotificationCompat.Builder notification = new NotificationCompat.Builder(context)
    .setContentTitle("Title")
    .setContentText("your_message")
    .setContentIntent(pendingIntent)
    .setAutoCancel(true)
    .setPriority(NotificationCompat.PRIORITY_HIGH);

notification.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
long[] pattern = {500,500,500,500};
notification.setVibrate(pattern);

Upvotes: 3

Related Questions