I had a custom notification with a text view and a button at it.I want to change the text of TextView when the button clicked.
I made a broadcast that when the button clicked the program start to run that broadcast,the codes of broadcast is :
package com.mori.sepid.notifications;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.ViewGroup;
import android.app.Notification;
import android.widget.RemoteViews;
import android.app.NotificationManager;
import android.widget.TextView;
public class button_broadcast_resiver extends BroadcastReceiver {
public button_broadcast_resiver() {
}
#Override
public void onReceive(Context context, Intent intent) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.customnoti1);
remoteViews.setTextViewText(R.id.text,"Hi morteza");
NotificationManager noti=
(NotificationManager)context.getSystemService(context.NOTIFICATION_SERVICE);
}
}
as you see the code above,I made an object from RemoteView and put the selected text to TextView but I don't know how to update this change to the notification panel.
The code below explains how to create your own notification:
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notification = new NotificationCompat.Builder(context)
//here you set icon which will be displayed on top bar
.setSmallIcon(R.drawable.your_ic_notification)
//here is title for your notification
.setContentTitle("tite"/*your notification title*/)
//here is a content which will be displayed when someone expand action bar
.setContentText("Some example context string"/*notifcation message*/)
.build();
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(1000/*some int notification id*/, notification);
Additionally if you want to add your Remote view you can use method:
setContent(RemoteViews remoteView)
from Notification.Builder class
Related
Is there a way to make android notification stay on screen for over 4 seconds.
For example:
Phone App has notification for incoming call, when this notification shows up (Incoming call) it does not leave the screen until the user is interacting with the notification (Accept call / deny call / go to phone app). However I have not seen a way to make notification last on screen (foreground) for over 4 seconds. Even when i am setting the FLAG_INSISTENT and FLAG_NO_CLEAR the notification last visible on screen for 4 - 5 seconds...
FLAG_INSISTENT - keeps beeping and vibrate on high priority notification but still the notification is going off the screen after 4 seconds and the user need to drag bar down in order to "see" the notification.
I am looking for a way to put a notification the same way that the phone app is doing it meaning keep the notification ON SCREEN until the user interacts with it.
Here is an example of main activity:
My MainActivity.java:
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private NotificationManagerCompat notificationManager;
#RequiresApi(api = Build.VERSION_CODES.O)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notificationManager = NotificationManagerCompat.from(this);
Intent YesReceive = new Intent(this, com.eddieharari.notifi.Data.class);
YesReceive.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntentYes = PendingIntent.getActivity(this,12345, YesReceive,
0);
Notification notification = new NotificationCompat.Builder(this, App.CHANNEL_1_ID)
.setSmallIcon(R.drawable.ic_launcher_background)
.setContentTitle("My Notify TITLE")
.setContentText("This is my notification Message")
.setPriority(NotificationManagerCompat.IMPORTANCE_HIGH)
.setCategory(NotificationCompat.CATEGORY_MESSAGE)
.setOngoing(false)
.setAutoCancel(true)
.setContentIntent(pendingIntentYes)
.setPriority(Notification.PRIORITY_MAX)
.build();
notification.flags = notification.flags | Notification.FLAG_INSISTENT | Notification.FLAG_NO_CLEAR;
notificationManager.notify(1, notification);
}
}
My App.java (where i set Notification Channel)
import android.app.Application;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.os.Build;
public class App extends Application {
public static final String CHANNEL_1_ID = "channel1";
#Override
public void onCreate() {
super.onCreate();
createNotificationChannels();
}
private void createNotificationChannels(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel1 = new NotificationChannel(CHANNEL_1_ID, "Channel 1", NotificationManager.IMPORTANCE_HIGH);
channel1.setDescription("This is channel 1");
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(channel1);
}
}
}
I FOUND OUT THE FOLLOWING:
Fullscreen intent inside the notification with high priority = true (second argument for the intent) leaves the notification on top of the screen till user interaction !
When using FullScreen intents don't forget to ask for fullScreenIntent permissions within the manifest....
First of all I would like to thank you for clicking on this thread.
Thank you so much for taking your time to read this.
I was planning for the application push a notification on a specific time.
I followed some tutorials online but it seems it doesn't work.
There is no error message on the code itself but no notification is coming out.
Here's my "Notification_receiver.java" file:
package com.example.reviewerapplication;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
public class Notification_receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context,Chapterchoice.class);
String daily10 = "daily";
intent.putExtra("daily",daily10);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Sample Title")
.setContentText("same message")
.setAutoCancel(true);
if (intent.getAction().equals("MY_NOTIFICATION_MESSAGE")) {
notificationManager.notify(100, builder.build());
}
}
}
This is what is on my MainActivity:
//Daily Notification
val calendar = Calendar.getInstance()
calendar.set(Calendar.HOUR_OF_DAY,21)
calendar.set(Calendar.MINUTE,53)
calendar.set(Calendar.SECOND,0)
val intent2 = Intent(this, Notification_receiver::class.java)
intent2.setAction("MY_NOTIFICATION_MESSAGE")
var pendingIntent = PendingIntent.getBroadcast(this,100,intent2,PendingIntent.FLAG_UPDATE_CURRENT )
var alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.timeInMillis,AlarmManager.INTERVAL_DAY,pendingIntent)
I actually got it fixed just now.
my issue is that I used "activity" in the Manifest instead of using "receiver"
Edit: It's fixed even though my code isn't perfect. I forgot to register this BroadCastReceiver in my Android Manifest file. Face Palm.
I'm having trouble getting my notification to work. It would help if I get some meaningful error message but I have no idea how to debug this.
Here's the code of the broadcast receiver:
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
public class ThisDoesNotWork extends BroadcastReceiver{
private Intent myIntent;
private PendingIntent myPending;
private static final int MY_NOTIFICATION_ID = 1;
private final CharSequence text1 = "Text1";
private final CharSequence text2 = "Text2!";
#Override
public void onReceive(Context context, Intent intent) {
myIntent = new Intent(context, MainActivity.class);
myPending = PendingIntent.getActivity(context, 0, myIntent,
Intent.FLAG_ACTIVITY_NEW_TASK);
Notification.Builder myNote = new Notification.Builder(context).setTicker(
"Take a Selfie!")
.setSmallIcon(android.R.drawable.stat_sys_warning)
.setAutoCancel(true).setContentTitle(text1)
.setContentText(text2)
.setContentIntent(myPending);
NotificationManager mNotify = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Log.i("Receiver", "Notification created.");
mNotify.notify(MY_NOTIFICATION_ID,myNote.getNotification());
Log.i("Receiver", "Notification sent.");
}
}
My program compiles but this notification gets ignored.
The error message I get is: Ignoring notification with icon==0; Notification(contentView =null vibrate=null...
Can anyone bring me 1 step closer to a solution?
Per the required notification contents, you must include:
A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()
It appears you are missing the content title, causing your notification to get ignored.
How do I get a message as a push notification at 8:00 AM in Android using titanium? The message is already stored in a SQLite database.
try this module to get local notification.
https://github.com/benbahrenburg/benCoding.AlarmManager
get your message from db and notify
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
public class MainActivity extends Activity {
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
showNotification("This is My Text");
}
private void showNotification(String message) {
// In this sample, we'll use the same text for the ticker and the expanded notification
CharSequence text =message;
// Set the icon, scrolling text and timestamp
Notification notification = new Notification(R.drawable.ic_launcher, text,
System.currentTimeMillis());
// The PendingIntent to launch our activity if the user selects this notification
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
// PendingIntent contentIntent = PendingIntent.getActivity(getApplicationContext(),0,
// new Intent(),PendingIntent.FLAG_UPDATE_CURRENT);
// Set the info for the views that show in the notification panel.
notification.setLatestEventInfo(this, message,
text, contentIntent);
NotificationManager mNM;
mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
int NOTIFICATION = 0;
// Send the notification.
mNM.notify(NOTIFICATION, notification);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
In the bottom right corner of my standard Android notification I will not see time (eg 12:00) rather than a pattern like that: 11/1/16. Always 3 numbers diveded by "/". Anybody know the problem?
I don't khow, how you make your notification but i think you should use : System.currentTimeMillis()
Example :
package com.test;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class TestActivity extends Activity {
private NotificationManager notificationManager;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
showNotification("My notification",android.R.id.text1);
}
private void showNotification(CharSequence text, int idNotify) {
Notification notification = new Notification(R.drawable.icon, text, System.currentTimeMillis());
Intent intent = new Intent();
intent.setClassName("com.test","com.test.TestActivity");
PendingIntent contentIntent = PendingIntent.getActivity(TestActivity.this, 0,intent,0);
notification.setLatestEventInfo(this, "My Notification",text, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(idNotify, notification);
}
}