I'm hoping this issue is simply something to do with the emulator, basically ive got a Service that sets a timer, as soon as the timertask runs the following code is supposed to execute:
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager)getSystemService(ns);
int icon = R.drawable.androidapplogo;
CharSequence tickerText = "Ticker Text";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "My Notification";
CharSequence contentText = "Hello World!";
//Do i need this?
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, null);
mNotificationManager.notify(1, notification);
The issue i have, is that when the notification gets called, it appears in the status bar, but when i click on the icon, it just has a blue circle. I dont own an android phone so im guessing that means its trying to load something.
Btw, ive pretty much lifted the code from here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
So my question is two fold:
- What is happening, is it failing to load something?
- Any ideas on how to fix it, or would it be worth finding a new form of notification
EDIT: Im sorry guys, found the issue was that you needed to drag down the Notification Window - i rarely use android x)
package com.androidtest.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SimpleNotification extends Activity {
private NotificationManager mNotificationManager;
private int SIMPLE_NOTFICATION_ID;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(R.drawable.android,
"New Alert, Click Me!", System.currentTimeMillis());
Button start = (Button) findViewById(R.id.notifyButton);
Button cancel = (Button) findViewById(R.id.cancelButton);
start.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Context context = getApplicationContext();
CharSequence contentTitle = "TITLES....";
CharSequence contentText = "TEXT FOR DETAIL";
Intent notifyIntent = new Intent(
android.content.Intent.ACTION_VIEW, Uri
.parse("CONTACT NO / OR ANY OTHER ACTION WHICH YOU WANT ON NOTIfic. ICON TAP"));
PendingIntent intent = PendingIntent.getActivity(
SimpleNotification.this, 0, notifyIntent,
android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
notifyDetails.setLatestEventInfo(context, contentTitle,
contentText, intent);
mNotificationManager.notify(SIMPLE_NOTFICATION_ID,
notifyDetails);
}
});
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mNotificationManager.cancel(SIMPLE_NOTFICATION_ID);
}
});
}
}
XML Test file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="#+id/notifyButton"
android:text="#string/notify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
></Button>
<Button
android:id="#+id/cancelButton"
android:text="#string/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
></Button>
</LinearLayout>
pending intent to call some intent on notification tap..gve ans frm whtevr i understand ur quest
leave comment if any prob with the code...
Related
I'm using the following code to generate a mediaPlayer notification:
package com.app1.notificationtemplate;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Handler;
import android.widget.RemoteViews;
import androidx.core.app.NotificationCompat;
public class NotificationGenerator {
public static final int NOTIFICATION_ID_OPEN_ACTIVITY = 9;
public static final String NOTIFY_PREVIOUS = "com.a.b.previous";
public static final String NOTIFY_DELETE = "com.a.b.delete";
public static final String NOTIFY_PAUSE = "com.a.b.pause";
public static final String NOTIFY_PLAY = "com.a.b.play";
public static final String NOTIFY_NEXT = "com.a.b.next";
public static void openActivityNotificaton(Context context) {
NotificationCompat.Builder nc = new NotificationCompat.Builder(context,
MainActivity.channelID);
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(context, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
nc.setContentIntent(pendingIntent);
nc.setSmallIcon(R.mipmap.ic_launcher);
nc.setAutoCancel(true);
nc.setContentTitle("Notification Demo");
nc.setContentText("Click please");
nm.notify(NOTIFICATION_ID_OPEN_ACTIVITY, nc.build());
}
public static void customBigNotification(Context context){
final RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.big_notification);
NotificationCompat.Builder nc = new NotificationCompat.Builder(context, MainActivity.channelID);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(context, MainActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
nc.setContentIntent(pendingIntent);
nc.setSmallIcon(R.drawable.play);
nc.setAutoCancel(true);
nc.setCustomBigContentView(expandedView);
nc.setContentTitle("MusicPlayer");
nc.setPriority(NotificationCompat.PRIORITY_MAX);
nc.setContentText("Control AUdio");
//expandedView.setTextViewText(R.id.text_song_name,"Adele");
setListeners(expandedView, context);
Notification notification = nc.build();
notification.flags = NotificationCompat.FLAG_ONGOING_EVENT|NotificationCompat.FLAG_FOREGROUND_SERVICE
| NotificationCompat.FLAG_NO_CLEAR|NotificationCompat.FLAG_BUBBLE ;
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
nm.notify("newNotif", 1, nc.build());
}
public static void setListeners(RemoteViews view, Context context){
Intent previous = new Intent(NOTIFY_PREVIOUS);
Intent delete = new Intent(NOTIFY_DELETE);
Intent pause = new Intent(NOTIFY_PAUSE);
Intent next = new Intent(NOTIFY_NEXT);
Intent play = new Intent(NOTIFY_PLAY);
PendingIntent pPrevious = PendingIntent.getBroadcast(context, 0,
previous, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPrevious, pPrevious);
PendingIntent pDelete = PendingIntent.getBroadcast(context, 0,
delete, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnDelete, pDelete);
PendingIntent pPause = PendingIntent.getBroadcast(context, 0,
pause, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPause, pPause);
PendingIntent pPlay = PendingIntent.getBroadcast(context, 0,
play, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnPlay, pPlay);
PendingIntent pNext = PendingIntent.getBroadcast(context, 0,
next, PendingIntent.FLAG_UPDATE_CURRENT);
view.setOnClickPendingIntent(R.id.btnNext, pNext);
}
}
I have custom notification views (RemoteViews) setup for the notification as well.
but, when I click play, I'm not sure how to update the play view to a pause view. Currently, I'm just setting the visibility of the play button to gone. But, is turning on and off the visibility turnwise the only option? And how do I do that anyways(the tutorial I've followed doesn't really show this). But, I think it'd be better if we could replace the image in the button which would avoid possible blinking or flickering. I've also gotta update the Remote view's artist and song Title name, but have no idea to do so. Please tell me how to update views in the RemoteView according to user interaction
Also, to communicate from the notification to the Service is using Broadcasts the only option? What other options do I have?
You need to create a new notification with the updated data and use NotificationManager to update it. No need to start service again. You have a running foreground service, all you have to do is update your UI via notify.
So in your Broadcast Receiver call your openActivityNotificaton() function with updated data, according to what view was clicked inside the notification. You can have one Receiver with a unique action for each view click, and update notification accordingly. I tested and it does not cause a blink, the notification is updated smoothly.
The code for your Receiver would be something like this:
const val NOTIFICATION_TITLE_INTENT_ACTION = "notification_title_intent_action"
const val NOTIFICATION_ARROW_INTENT_ACTION = "notification_arrow_intent_action"
class NotificationInteractionReceiver : BroadcastReceiver() {
private lateinit var notificationsFactory: NotificationFactory
override fun onReceive(context: Context, intent: Intent) {
notificationsFactory = NotificationFactory(context)
// Update title according to what button was clicked
val newTitle = when (intent.action) {
NOTIFICATION_TITLE_INTENT_ACTION -> "title clicked"
else -> "arrow clicked"
}
// Pass new title to class responsible for showing notification
notificationsFactory.showNotification(context, s)
}
companion object {
fun getIntent(context: Context, action: String): Intent {
val intent = Intent(context, NotificationInteractionReceiver::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
intent.action = action
return intent
}
}
}
I know that this type of question asked but they are unable to solve my problem, Hence I am asking it again for having exact solution to my problem, I am using android studio 3.0.1 for creating simple notification application, when i run this application it shows me errors as show as follow:
Error:(40, 25) error: PendingIntent() is not public in PendingIntent; cannot be accessed from outside package
and my MainActivity.java as follow
package com.example.ram.notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
NotificationCompat.Builder notification;
private static final int uniqueID = 45612;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notification = new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
}
public void ClickNotification(View view){
//build the notification
notification.setSmallIcon(R.drawable.myimage);
notification.setTicker("this is the ticker");
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle("Here is the title");
notification.setContentText("I am the body of your notificattion");
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = new PendingIntent().getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
//build notification and issues it
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.notify(uniqueID, notification.build());
}
}
Please help me what should i do?
Simply delete "new" keyword in 40 row
must be
pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
This is a code for creating a notification by click a button and then turn to another class. The second class it totally empty. I don't know why it doesn't appear even it cannot turn to another class.
I have already checked the "sittings" of this file and the "notification" is "on". Could anyone help me to check it?
MainActivity.class
package com.example.ww.lab;
import android.app.Notification;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.NotificationManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private int id = 1;
private NotificationCompat.Builder notification_builder;
private NotificationManagerCompat notification_manager;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
notification_manager.notify(id, notification_builder.build());
}
});
Intent open_activity_intent = new Intent(this, NotificationActivity.class);
PendingIntent pending_intent = PendingIntent
.getActivity(this, 0, open_activity_intent, PendingIntent.FLAG_CANCEL_CURRENT);
notification_builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notification Title")
.setContentText("Notification Body")
.setAutoCancel(true)
.setContentIntent(pending_intent);
notification_manager = NotificationManagerCompat.from(this);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ww.lab.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do it!"
android:id="#+id/button"
android:onClick="sendNotification"/>
</android.support.constraint.ConstraintLayout>
As you have stated your compiled sdk version is 26. You need to set Channel of your notification before posting it. So it will be like
NotificationManager notification_manager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String chanel_id = "3000";
CharSequence name = "Channel Name";
String description = "Chanel Description";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = new NotificationChannel(chanel_id, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.BLUE);
notification_manager.createNotificationChannel(mChannel);
notification_builder = new NotificationCompat.Builder(this, chanel_id)
} else {
notification_builder = new NotificationCompat.Builder(this);
}
notification_builder.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Notification Title")
.setContentText("Notification Body")
.setAutoCancel(true)
.setContentIntent(pending_intent);
For more please visit Developer Site
I am trying to build a simple app that sends notifications to the device but only on a specific day/time. I have been able to get the notification part to work but cant seem to get the day & time problem solved. Any ideas?
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class NotificationsActivity extends Activity {
int notificationID = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClick(View view) {
displayNotification();
}
protected void displayNotification()
{
Intent i = new Intent(this, NotificationView.class);
i.putExtra("notificationID", notificationID);
PendingIntent pendingIntent =
PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(
R.drawable.ic_launcher,
"Reminder: Meeting starts in 5 minutes",
System.currentTimeMillis());
CharSequence from = "System Alarm";
CharSequence message = "Meeting with customer at 3pm...";
notif.setLatestEventInfo(this, from, message, pendingIntent);
notif.vibrate = new long[] { 100, 250, 100, 500};
nm.notify(notificationID, notif);
}
}
You need to use an AlarmManager to schedule these events. Then, from the callbacks, you show the notifications.
See for example Creating a Notification at a particular time through Alarm Manager
Be careful, as AlarManager will be cleared when the device reboots (you may need to re-register then on boot completed if necessary).
See this link Notification in Android Using AlarmManager, BoradCastReceiver & Services
It may be help you.
I have the content module loaded, the specific error I'm getting is: The constructor Intent(new View.OnClickListener(){}, Class<ContactWidget>) is undefined
Any ideas on this? I got this from the tutorial here: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
package com.example.contactwidget;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ContactWidget extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button calc1 = (Button) findViewById(R.id.calc_button_1);
calc1.setOnClickListener(buttonListener);
setContentView(R.layout.main);
}
private static final int HELLO_ID = 1;
private OnClickListener buttonListener = new OnClickListener() {
public void onClick (View v) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
CharSequence ticketBrief = "Button Pressed Brief";
CharSequence ticketTitle = "Button pressed";
CharSequence ticketText = "You pressed button 1";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, ticketBrief, when);
Intent notificationIntent = new Intent(this, ContactWidget.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), ticketTitle, ticketText, contentIntent);
mNotificationManager.notify(HELLO_ID, notification);
}
};
}
Change this:
new Intent(this, ContactWidget.class);
to
new Intent(ContactWidget.this, ContactWidget.class);
The error happens because, in that case, this is referencing the instance of OnClickListener, but the Intent's constructor expects a Context. The context you have to pass is the reference to the activity itself, thus you have to access it explicitly using ContactWidget.this.
The context passed in could be from a remote application host... apparently you need the app context from your app, the one with your class (your broadcast reciever for instance).
Intent intent = new Intent(context.getApplicationContext(), WidgetryBroadcastReceiver.class);
instead of
Intent intent = new Intent(context, WidgetryBroadcastReceiver.class);
go figure.
yeah that workerd for me aswell for the error in
Geocoder gc = new Geocoder(this, Locale.getDefault());