通知声音不适用于api 10 android

时间:2015-12-09 09:21:58

标签: android notifications

我使用此功能显示状态notification。一切都是正确的但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);


    }

我在此之后设置振动但振动也不起作用:(

2 个答案:

答案 0 :(得分:3)

您可以使用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);

答案 1 :(得分:2)

您应该使用NotificationCompat.Builder,因为Notification类在API级别20及更高版本中进行了描述

并使用这段代码我希望它适用于你

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());

通知音可以正常使用 Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);             builder.setSound(alarmSound);