【问题标题】:Create push notification settings like Whatsapp "notification" settings android创建推送通知设置,例如 Whatsapp“通知”设置 android
【发布时间】:2014-03-29 20:01:37
【问题描述】:

我是开发 android 应用程序和学习开发基本应用程序和功能的新手。 我想像 WhtsApp 一样创建通知设置,即

  1. 通知音(所有默认音的列表)
  2. 振动:默认(或我自己的自定义:关闭、默认、短、长)

当设备收到新通知时,我想要这个应用程序的基本功能。 我正在使用 GCM 发送和接收通知。 这将是很大的帮助。提前致谢。

我想要的通知菜单是这样的: http://i.stack.imgur.com/vanda.jpg

编辑:

SettingsActivity.java

import android.app.Activity;
import android.app.Notification;
import android.app.Service;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Menu;
import android.widget.CompoundButton; 
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class SettingsActivity extends Activity {

Vibrator vibrate;
ToggleButton tb;
boolean status= false;
public static final String MyPREFERENCES = "MyPrefs";
SharedPreferences prefs;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    tb = (ToggleButton) findViewById(R.id.toggleButton1);
    prefs = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    if (prefs.contains("vibrate")) {
        status=Boolean.parseBoolean(prefs.getString("vibrate", ""));
        if(status){
            tb.setChecked(true);
        }else{
            tb.setChecked(false);
        }
    }

    tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            // TODO Auto-generated method stub
            if (isChecked) {
                Toast.makeText(getApplicationContext(), "ON",
                        Toast.LENGTH_LONG).show();
                status= true;
                Editor editor = prefs.edit();
                editor.putString("vibrate", status+"");
                editor.commit();
                PreferencesData.saveString(getApplicationContext(), "vibrate", status+"");
            } else {
                Toast.makeText(getApplicationContext(), "OFF",
                        Toast.LENGTH_LONG).show();
                status= false;
                Editor editor = prefs.edit();
                editor.putString("vibrate", status+"");
                editor.commit();
                PreferencesData.saveString(getApplicationContext(), "vibrate", status+"");

            }
        }
    });
}

public boolean isVibrate(){

    return status;
}
}

GCMIntentService.java

 //some code

private static void generateNotification(Context context, String message) {
    int icon = R.drawable.ic_launcher;
    long when = System.currentTimeMillis();
    NotificationManager notificationManager = (NotificationManager)
            context.getSystemService(Context.NOTIFICATION_SERVICE);
    Notification notification = new Notification(icon, message, when);

    String title = context.getString(R.string.app_name);

    Intent notificationIntent = new Intent(context, MainActivity.class);
    // set intent so it does not start a new activity
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent =
            PendingIntent.getActivity(context, 0, notificationIntent, 0);
    notification.setLatestEventInfo(context, title, message, intent);
    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    // Play default notification sound
    notification.defaults |= Notification.DEFAULT_SOUND;

    SettingsActivity stng = new SettingsActivity();
    Vibrator vibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);

    boolean vibStatus = stng.isVibrate();

    //notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

    // Vibrate if vibrate is enabled
    if(vibStatus){
    notification.defaults |= Notification.DEFAULT_VIBRATE;
    vibrator.vibrate(500);
    }
    else{
        vibrator.cancel();
    }
    notificationManager.notify(0, notification);      

}

【问题讨论】:

标签: android notifications push-notification android-notifications whatsapp


【解决方案1】:

创建一个Constant Class 喜欢

public class Constant {
public static boolean IS_VIBRATE = false;
}

现在,实现你Toggle Button OnCheckedChangeListenerlike

tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
        // TODO Auto-generated method stub
        if (isChecked) {
            Toast.makeText(getApplicationContext(), "ON",
                    Toast.LENGTH_LONG).show();
           Constant.IS_VIBRATE = true;
        } else {
            Toast.makeText(getApplicationContext(), "OFF",
                    Toast.LENGTH_LONG).show();
          Constant.IS_VIBRATE = false;
        }
    }

现在,将您的Notification Vibrate 条件设置为

 private static void generateNotification(Context context, String message) {
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager)
        context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);

String title = context.getString(R.string.app_name);

Intent notificationIntent = new Intent(context, MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
        Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
        PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;

// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;

//notification.sound = Uri.parse("android.resource://" + context.getPackageName() + "your_sound_file_name.mp3");

// Vibrate if vibrate is enabled
if(Constant.IS_VIBRATE){
 notification.defaults |= Notification.DEFAULT_VIBRATE;
 notificationManager.notify(0, notification);  
 }else{
 notificationManager.notify(0, notification);  
 }
}

按照我的回答试试,让我知道

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多