I'm trying to change the notification sound by using channels, and it works perfectly.
I know the channels just support android 8.0 and above but I have a case to turn the app into android 7. So is there a way to change the notification sound in this version?
MainActivity.java
package com.myapp;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactActivityDelegate;
import com.facebook.react.ReactRootView;
import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
import android.os.Bundle;
import android.media.AudioAttributes;
import android.media.SoundPool;
import android.net.Uri;
import android.content.ContentResolver;
import androidx.core.app.NotificationCompat;
public class MainActivity extends ReactActivity {
/**
* Returns the name of the main component registered from JavaScript. This is used to schedule
* rendering of the component.
*/
#Override
protected String getMainComponentName() {
return "myApp";
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel("new_order", "myApp", NotificationManager.IMPORTANCE_HIGH);
notificationChannel.setShowBadge(true);
notificationChannel.setDescription("");
AudioAttributes att = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.setContentType(AudioAttributes.CONTENT_TYPE_SPEECH)
.build();
notificationChannel.setSound(Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/raw/bell"), att);
notificationChannel.enableVibration(true);
notificationChannel.setVibrationPattern(new long[]{400, 400});
notificationChannel.setLockscreenVisibility(NotificationCompat.VISIBILITY_PUBLIC);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(notificationChannel);
}
}
#Override
protected ReactActivityDelegate createReactActivityDelegate() {
return new ReactActivityDelegate(this, getMainComponentName()) {
#Override
protected ReactRootView createRootView() {
return new RNGestureHandlerEnabledRootView(MainActivity.this);
}
};
}
}
Related
I'm working on an app that shows notifications and notification sounds to the user. The user can select if he wants to see them by 2 switch buttons on the settings activity. I want to open the activity Visitor when the user clicks on the notification. I wrote the following code but this doesn't happen, what should I write instead?
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import java.util.Set;
import androidx.annotation.RequiresApi;
import static android.app.PendingIntent.getActivity;
import static android.content.Context.NOTIFICATION_SERVICE;
import static com.example.myevents.R.drawable.notification;
public class Settings extends AppCompatActivity {
Switch simpleswitch1;
Switch simpleswitch2;
private Notification notification;
NotificationManager manager;
Notification myNotication;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
simpleswitch1 = (Switch) findViewById(R.id.simpleswitch1);
simpleswitch2 = (Switch) findViewById(R.id.simpleswitch2);
simpleswitch1.setChecked(false);
simpleswitch2.setChecked(false);
simpleswitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#TargetApi(Build.VERSION_CODES.O)
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){{
int notifyID = 1;
String CHANNEL_ID = "my_channel_01";// The id of the channel.
CharSequence name = "channel 1";// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
Intent intent = new Intent(Settings.this, Visitor.class);
intent.putExtra("yourpackage.notifyId", notifyID);
PendingIntent pIntent = PendingIntent.getActivity(Settings.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create a notification and set the notification channel.
Notification notification =
new NotificationCompat.Builder(Settings.this)
.setSmallIcon(R.drawable.notification)
.setContentTitle("TITLE")
.setContentText("TEXT")
.setChannelId(CHANNEL_ID).build();
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(mChannel);
// Issue the notification.
mNotificationManager.notify(notifyID , notification);
}
As described in the Android documentation, you need to set the intent that will fire when the user taps the notification on your builder:
NotificationCompat.Builder(Settings.this)
.setSmallIcon(R.drawable.notification)
.setContentTitle("My Events")
.setContentText("Νέα εκδήλωση κοντά σας!")
.setContentIntent(pIntent)
.setChannelId(CHANNEL_ID).build();
See : https://developer.android.com/training/notify-user/build-notification#click
I am trying to implement an alarm manager that will be called every day to update a database. I started by trying the set Function of the alarmManager and it worked to trigger the alarm at a certain time. Then I moved on to try and use setRepeating to trigger the alarm on a daily basis. The setRepeating function which is done to Repeat every minute just for testing is only working the first time and is not doing the function repeatedly.
Here is my BroadcastReceiver:
package com.example.bleh.myapplication;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.example.bleh.myapplication.DB.AppDatabase;
import com.example.bleh.myapplication.DB.Plan;
import com.example.bleh.myapplication.DB.User;
import com.example.bleh.myapplication.Utils1.FormulaUtils;
import com.github.lzyzsd.circleprogress.DonutProgress;
public class AlarmReceiver extends BroadcastReceiver {
private static final String DEBUG_TAG = "AlarmReceiver";
public AppDatabase mydb;
Plan plan;
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
mydb = AppDatabase.getInstance(context);
final long planid = intent.getExtras().getLong("planid");
final long userid = intent.getExtras().getLong("uid");
final String requirements = intent.getExtras().getString("requirements");
Log.wtf("PlanId: ",planid+"");
Log.wtf("UserId: ",userid+"");
Log.wtf("Requirements",requirements);
plan = mydb.getPlanDao(context).getPlanById((int) planid);
final User user = mydb.getUserDao(context).getUserById((int) userid);
plan.setCurrentWeight(FormulaUtils.reCalculateWeight(plan.getCurrentWeight(), Double.parseDouble(requirements)));
plan.setBmr(Double.parseDouble(FormulaUtils.calculateBmr(user.getSex(), plan.getCurrentWeight(), user.getHeight(), user.getBirthDay())));
plan.setNbOfDays(plan.getNbOfDays() - 1);
mydb.getPlanDao(context).update(plan);
String requirement = FormulaUtils.CalulcateDailyRequirements(plan.getWorkoutPerWeek(), plan.getBmr());
String day = plan.getNbOfDays() + "";
float progress = 0F;
// Start feature2 activity with updated data
Intent updateFeature2Intent = new Intent(context, feature2.class);
updateFeature2Intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP); // Add this flag
updateFeature2Intent.putExtra("requirement", requirement);
updateFeature2Intent.putExtra("day", day);
updateFeature2Intent.putExtra("progress", progress);
context.startActivity(updateFeature2Intent);
}
}
Here is my Feature Activity:
package com.example.bleh.myapplication;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import com.example.bleh.myapplication.DB.AppDatabase;
import com.example.bleh.myapplication.DB.Exercise;
import com.example.bleh.myapplication.DB.Food;
import com.example.bleh.myapplication.DB.Plan;
import com.example.bleh.myapplication.DB.User;
import com.example.bleh.myapplication.Utils1.FormulaUtils;
import com.github.lzyzsd.circleprogress.DonutProgress;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class feature2 extends AppCompatActivity {
public AppDatabase mydb;
TextView BMR,requirements,days;
Button addfood,addex,nextday;
LinearLayout mainLayout;
Button Meas,Bluetooth;
DonutProgress donutProgress;
Plan plan;
// Add this method
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// Get data from intent
String requirement = intent.getStringExtra("requirement");
String day = intent.getStringExtra("day");
float progress = intent.getFloatExtra("progress", 0F);
// Update UI
TextView requirements = findViewById(R.id.requirements);
TextView Days = findViewById(R.id.days);
DonutProgress dailyProgress = findViewById(R.id.donut_progress);
requirements.setText(requirement);
Days.setText(day);
dailyProgress.setProgress(progress);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_feature2);
Intent intent = getIntent();
requirements = findViewById(R.id.requirements);
donutProgress = findViewById(R.id.donut_progress);
days = findViewById(R.id.days);
final long planid = intent.getExtras().getLong("planid");
final long userid = intent.getExtras().getLong("uid");
mydb = AppDatabase.getInstance(feature2.this);
plan = mydb.getPlanDao(feature2.this).getPlanById((int) planid);
try {
requirements.setText(FormulaUtils.CalulcateDailyRequirements(plan.getWorkoutPerWeek(), plan.getBmr()));
}
catch (Exception ex)
{
Log.wtf("There is no plan","!");
}
Intent intent1 = new Intent(this, AlarmReceiver.class);
intent1.putExtra("uid", userid);
intent1.putExtra("planid", planid);
intent1.putExtra("requirements",requirements.getText().toString());
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getTimeZone("GMT"));
updateTime.set(Calendar.HOUR_OF_DAY, 8);
updateTime.set(Calendar.MINUTE, 3);
updateTime.set(Calendar.SECOND,0);
Date milliseconds = updateTime.getTime();
long millis = milliseconds.getTime();
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
intent1, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC, millis ,60000, pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
}
}
Here is my Manifest Part:
<receiver android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true">
</receiver>
I had encountered this problem earlier with my app
Repeating alarms will not trigger for api > 23 if phone is in idle state. Refer Schedue Repeating alarms
Doze mode also blocks the alarm.
What you can do is to use setExact and setExactAndAllowWhileIdle for api > 23 and use your own logic to repeat the alarm.
if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
} else {
alarmManager.setExact(RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
}
Trigger the alarm again in the receiver or in the activity which is triggered by the alarm manager.
In your case set the alarm again in the AlarmReceiver for next day.
I'd recommend you to read about Doze mode and this topic. Also you can use this lib by Evernote. It helps to handle scheduling of background tasks.
This question already has answers here:
How to sound notifications sound when new Message arrives from Firebase Realtime Data base
(4 answers)
Closed 4 years ago.
I'm using firebase data base to create my chat applicaiton. Now that I have sucessfully completed my chat application, but when new message arrives I would like to notify user with sound and NOtification in Notification bar even when the app is not running.
I used the below code to do that
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());
builder.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
NotificationManager manager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, builder.build());
But it only set alarm when i open the chat activity, then aftearwards when new message arrives it does nothing.
Here is my chat activity code
package com.nepalpolice.mnemonics.chat;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.media.ToneGenerator;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.provider.Settings;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.messaging.RemoteMessage;
import com.nepalpolice.mnemonics.R;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* Created by filipp on 6/28/2016.
*/
public class Chat_Room extends AppCompatActivity{
private Button btn_send_msg;
private EditText input_msg;
private TextView chat_conversation;
private Toolbar mainToolbar;
private String user_name,room_name;
private DatabaseReference root ;
private String temp_key;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_room);
btn_send_msg = (Button) findViewById(R.id.btn_send);
input_msg = (EditText) findViewById(R.id.msg_input);
chat_conversation = (TextView) findViewById(R.id.textView);
user_name = getIntent().getExtras().get("user_name").toString();
room_name = getIntent().getExtras().get("room_name").toString();
mainToolbar = (Toolbar) findViewById(R.id.main_chat);
setSupportActionBar(mainToolbar);
getSupportActionBar().setTitle(" Room - "+room_name);
root = FirebaseDatabase.getInstance().getReference().child(room_name);
btn_send_msg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Map<String,Object> map = new HashMap<String, Object>();
temp_key = root.push().getKey();
root.updateChildren(map);
DatabaseReference message_root = root.child(temp_key);
Map<String,Object> map2 = new HashMap<String, Object>();
map2.put("name",user_name);
map2.put("msg",input_msg.getText().toString());
message_root.updateChildren(map2);
input_msg.getText().clear();
}
});
root.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
append_chat_conversation(dataSnapshot);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
append_chat_conversation(dataSnapshot);
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private String chat_msg,chat_user_name;
private void append_chat_conversation(DataSnapshot dataSnapshot) {
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()){
chat_msg = (String) ((DataSnapshot)i.next()).getValue();
chat_user_name = (String) ((DataSnapshot)i.next()).getValue();
chat_conversation.append(chat_user_name +" : "+chat_msg +" \n");
}
}
}
Here is my Firebase Data structure file
Firebase Data Structure
Any help is appreciated. Thanks in advance.
If you are using FCM, you need to understand this part of the doc before proceeding:
The onMessageReceived is provided for most message types, with the following exceptions which are listed below:
Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default
Messages with both notification and data payload, both background and foreground. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.
So if you need to decide the type of payload you re sending to the android device. You may want to go for Message with both Notifications and data payload so that onMessageRecieved() is invoked at all times.
You can find more details Here
Below is my code,I am using cordova Push Notification,I am trying to merge multiple push notification but its give me error, for single notification its working, i am unable to find what went wrong, kindly suggest
package org.apache.cordova.firebase;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.text.TextUtils;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
import java.util.Random;
private void sendNotification(String id, String title, String messageBody, Map<String, String> data, boolean showNotification)
{
Bundle bundle = new Bundle();
int notifyID = 1;
for (String key : data.keySet()) {
bundle.putString(key, data.get(key));
}
if (showNotification) {
Intent intent = new Intent(this, OnNotificationOpenReceiver.class);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id.hashCode(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri);
int numMessages = 0;
//This line gives me error:error: cannot find symbol
notificationBuilder.setContentText(currentText).setNumber(++numMessages);
notificationManager.notify(notifyID, notificationBuilder.build());
} else {
bundle.putBoolean("tap", false);
FirebasePlugin.sendNotification(bundle);
}
}
It looks like currentText that is used in the setContextText method isn't instantiated anywhere in your code.
After two days of tests we were able to partially work.
I downloaded the file, but I have no idea where to store it.
I tried to download a picture and the picture appears in the folder /all_downloads
I do not know how to store it in /sdcard/update.
import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.io.File;
public class Upgrade extends ActionBarActivity {
private long enqueue;
private DownloadManager dm;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upgrade);
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
long downloadId = intent.getLongExtra(
DownloadManager.EXTRA_DOWNLOAD_ID, 0);
Query query = new Query();
query.setFilterById(enqueue);
Cursor c = dm.query(query);
if (c.moveToFirst()) {
int columnIndex = c
.getColumnIndex(DownloadManager.COLUMN_STATUS);
if (DownloadManager.STATUS_SUCCESSFUL == c
.getInt(columnIndex)) {
}
}
}
}
};
registerReceiver(receiver, new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
public void onClick(View view) {
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Request request = new Request(
Uri.parse("http://android.vrt.ro/tv-update/v1.apk"));
enqueue = dm.enqueue(request);
}
public void showDownload(View view) {
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setDataAndType(Uri.parse("file:///sdcard/download/v1.apk"),
"application/vnd.android.package-archive");
startActivity(promptInstall);
}
}
Use this
request.setDestinationInExternalPublicDir("/updates", "update.apk");
Added in API level 9
public DownloadManager.Request setDestinationInExternalPublicDir (String dirType, String subPath)
Set the local destination for the downloaded file to a path within the
public external storage directory (as returned by
getExternalStoragePublicDirectory(String)).
The downloaded file is not scanned by MediaScanner. But it can be made
scannable by calling allowScanningByMediaScanner().
Parameters dirType the directory type to pass to
getExternalStoragePublicDirectory(String)` subPath the path within the
external directory, including the destination filename Returns this
object Throws IllegalStateException If the external storage directory
cannot be found or created.
You can also use this version
String updatePath = Environment.getExternalStorageDirectory() + File.separator + "Updates" + File.separator + "update.apk";
request.setDestinationUri(Uri.fromFile(new File(updatePath)))