无法设置在Android的推送通知的声音 [英] unable to set push notification sound in android

查看:194
本文介绍了无法设置在Android的推送通知的声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了推送通知的应用程序,我碰到GCM服务的消息,但推通知声音无法正常工作。我需要播放声音时,我越来越notification.I都张贴了我的code以下的人帮我这...

  //接收器类//


进口android.app.Activity;
进口android.app.NotificationManager;
进口android.content.ComponentName;
进口android.content.Context;
进口android.content.Intent;
进口android.media.RingtoneManager;
进口android.net.Uri;
进口android.support.v4.content.WakefulBroadcastReceiver;
进口android.widget.Toast;

公共类GcmBroadcastReceiver扩展WakefulBroadcastReceiver {

  @覆盖
  公共无效的onReceive(上下文的背景下,意图意图)
  {
    组件名排版=新单元名(context.getPackageName(),GCMNotificationIntentService.class.getName());

    startWakefulService(上下文,(intent.setComponent(化合物)));

    的setResult code(Activity.RESULT_OK);

    generateNotification(上下文);

    的System.out.println(收到推送通知);

    Toast.makeText(背景下,接到通知,Toast.LENGTH_LONG).show();
  }

私人无效generateNotification(上下文的背景下)
{
    // TODO自动生成方法存根
    / * NotificationManager mBuilder =(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);

    乌里notificationsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    mBuilder.setSound(notificationsound);
    * /
}

保护无效onHandleIntent(意向意图){
    // TODO自动生成方法存根

}
}




包com.everest.jewellerapp;

进口android.app.IntentService;
进口android.app.Notification;
进口android.app.NotificationManager;
进口android.app.PendingIntent;
进口android.content.Context;
进口android.content.Intent;
进口android.graphics.Color;
进口android.media.RingtoneManager;
进口android.net.Uri;
进口android.os.Bundle;
进口android.os.SystemClock;
进口android.support.v4.app.NotificationCompat;
进口android.util.Log;
进口com.google.android.gms.gcm.GoogleCloudMessaging;

公共类GCMNotificationIntentService扩展IntentService
{


  公共静态最终诠释NOTIFICATION_ID = 1;

  私人NotificationManager mNotificationManager;

  NotificationCompat.Builder建设者;

  公共GCMNotificationIntentService()
  {
    超级(GcmIntentService);
  }

  公共静态最终字符串变量=GCMNotificationIntentService;

  @覆盖
  保护无效onHandleIntent(意向意图)
  {
    捆绑额外= intent.getExtras();

    GoogleCloudMessaging GCM = GoogleCloudMessaging.getInstance(本);

    字符串为messageType = gcm.getMessageType(意向);

    如果(!extras.isEmpty())
    {
      如果(GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(为messageType))
      {
        sendNotification的(发送错误:+ extras.toString());
      }
      否则,如果(GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(为messageType))
      {
        sendNotification的(在服务器上删除的邮件:
            + extras.toString());
      }
      否则,如果(GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(为messageType))
      {

        的for(int i = 0;我3;;我++){
          Log.i(TAG,
              工作...+(I + 1)+/ 5 @
                  + SystemClock.elapsedRealtime());
          尝试
          {
            视频下载(5000);
          }
          赶上(InterruptedException的E)
          {
          }

        }
        Log.i(TAG,已完成的工作@+ SystemClock.elapsedRealtime());

        sendNotification的(信息从谷歌GCM服务器接收到的:+ extras.get(Config.MESSAGE_KEY));



        Log.i(TAG,收稿日期:+ extras.toString());
      }
    }
    GcmBroadcastReceiver.completeWakefulIntent(意向);
  }

  私人无效sendNotification的(弦乐味精)
  {
    Log.d(TAG,preparing发送通知...:+味精);

    mNotificationManager =(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(此,0,新意图(本,MainActivity.class),0);

    NotificationCompat.Builder mBuilder =新NotificationCompat.Builder(本)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle(ijeweller)
        .setStyle(新NotificationCompat.BigTextStyle()。bigText(MSG))
        .setContentText(MSG);

    mBuilder.setContentIntent(co​​ntentIntent).setContentTitle()()setContentText。;
    mBuilder.setVibrate(新长[] {1000,1000,1000,1000,1000});
    mBuilder.setLights(Color.RED,3000,3000);


    乌里notificationsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    mBuilder.setSound(notificationsound);


    mNotificationManager.notify(NOTIFICATION_ID,mBuilder.build());

    Log.d(TAG,通知发送成功);
  }
}
 

权限

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>

<! -  GCM需要一个谷歌帐户。 - >
<使用-权限的Andr​​oid:名称=android.permission.GET_ACCOUNTS/>

<! - 进入休眠状态保持处理器接收到消息时。 - >
<使用-权限的Andr​​oid:名称=android.permission.WAKE_LOCK/>

<! - 创建一个自定义权限所以只有这个程序可以接收它的消息。 - >
<许可
    机器人:名称=com.everest.jewellerapp.permission.C2D_MESSAGE
    安卓的ProtectionLevel =签名/>

<使用-权限的Andr​​oid:名称=com.everest.jewellerapp.permission.C2D_MESSAGE/>

<  - !此应用程序有权限注册并接收数据信息。 - >
<使用-权限的Andr​​oid:名称=com.google.android.c2dm.permission.RECEIVE/>

<! - 网络状态的权限来检测网络状态 - >
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>

<! - 权限振动 - >
<使用-权限的Andr​​oid:名称=android.permission.VIBRATE/>

<使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE/>

<使用-权限的Andr​​oid:名称=android.permission.MODIFY_AUDIO_SETTINGS/>
      <使用-权限的Andr​​oid:名称=android.permission.CALL_PHONE/>
      <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_MMS/>
      <使用-权限的Andr​​oid:名称=android.permission.RECEIVE_SMS/>
      <使用-权限的Andr​​oid:名称=android.permission.READ_SMS/>
 

解决方案

我用的是默认的通知声音。也许这将工作:

  mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);
 

I have created the push notification application, I'm getting the message from GCM services but push notification sound is not working. I need to play sound when I'm getting notification.I have posted my code below anyone help me with this...

//receiver class//


import android.app.Activity;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.content.WakefulBroadcastReceiver;
import android.widget.Toast;

public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) 
  {
    ComponentName comp = new ComponentName(context.getPackageName(),GCMNotificationIntentService.class.getName());

    startWakefulService(context, (intent.setComponent(comp)));

    setResultCode(Activity.RESULT_OK);

    generateNotification(context);

    System.out.println("received push notification");

    Toast.makeText(context,"received notification",Toast.LENGTH_LONG).show();
  }

private void generateNotification(Context context)
{
    // TODO Auto-generated method stub
    /*NotificationManager mBuilder=(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);

    Uri notificationsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    mBuilder.setSound(notificationsound);
    */
}

protected void onHandleIntent(Intent intent) {
    // TODO Auto-generated method stub

}
}




package com.everest.jewellerapp;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.android.gms.gcm.GoogleCloudMessaging;

public class GCMNotificationIntentService extends IntentService
{


  public static final int NOTIFICATION_ID = 1;

  private NotificationManager mNotificationManager;

  NotificationCompat.Builder builder;

  public GCMNotificationIntentService() 
  {
    super("GcmIntentService");
  }

  public static final String TAG = "GCMNotificationIntentService";

  @Override
  protected void onHandleIntent(Intent intent) 
  {
    Bundle extras = intent.getExtras();

    GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(this);

    String messageType = gcm.getMessageType(intent);

    if (!extras.isEmpty()) 
    {
      if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) 
      {
        sendNotification("Send error: " + extras.toString());
      }
      else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) 
      {
        sendNotification("Deleted messages on server: "
            + extras.toString());
      } 
      else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) 
      {

        for (int i = 0; i < 3; i++) {
          Log.i(TAG,
              "Working... " + (i + 1) + "/5 @ "
                  + SystemClock.elapsedRealtime());
          try 
          {
            Thread.sleep(5000);
          } 
          catch (InterruptedException e)
          {
          }

        }
        Log.i(TAG, "Completed work @ " + SystemClock.elapsedRealtime());

        sendNotification("Message Received from Google GCM Server: "+ extras.get(Config.MESSAGE_KEY));



        Log.i(TAG, "Received: " + extras.toString());
      }
    }
    GcmBroadcastReceiver.completeWakefulIntent(intent);
  }

  private void sendNotification(String msg) 
  {
    Log.d(TAG, "Preparing to send notification...: " + msg);

    mNotificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);

    PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(this, MainActivity.class), 0);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .setContentTitle("ijeweller")
        .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
        .setContentText(msg);

    mBuilder.setContentIntent(contentIntent).setContentTitle("").setContentText("");
    mBuilder.setVibrate(new long[] { 1000, 1000, 1000, 1000, 1000 }); 
    mBuilder.setLights(Color.RED, 3000, 3000); 


    Uri notificationsound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    mBuilder.setSound(notificationsound);


    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());

    Log.d(TAG, "Notification sent successfully.");
  }
}

permissions

<uses-permission android:name="android.permission.INTERNET" />

<!-- GCM requires a Google account. -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />

<!-- Keeps the processor from sleeping when a message is received. -->
<uses-permission android:name="android.permission.WAKE_LOCK" />

<!-- Creates a custom permission so only this app can receive its messages. -->
<permission
    android:name="com.everest.jewellerapp.permission.C2D_MESSAGE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.everest.jewellerapp.permission.C2D_MESSAGE" />

<!-- This app has permission to register and receive data message. -->
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

<!-- Network State Permissions to detect Internet status -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<!-- Permission to vibrate -->
<uses-permission android:name="android.permission.VIBRATE" />

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 
      <uses-permission android:name="android.permission.CALL_PHONE"/>
      <uses-permission android:name="android.permission.RECEIVE_MMS" />
      <uses-permission android:name="android.permission.RECEIVE_SMS" />
      <uses-permission android:name="android.permission.READ_SMS" />

解决方案

I use the default notification sound. Maybe this will work:

mBuilder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI);

这篇关于无法设置在Android的推送通知的声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆