I'm implementing a notification service and I have problems to receive the messages in Android 2.3. I receive the messages in versions 4.0 and newer but not in 2.3. In logcat appears the following errors:
Could not find class 'com.google.android.gms.ads.internal.b.c', referenced from method com.google.android.gms.ads.internal.n.e.a
.
.
Could not find class 'android.app.Notification$Builder', referenced from method com.google.android.gms.common.l.a
What could be the problem? This is my method to send the notification:
private void sendNotification(String msg) {
MainActivity.notificationClicked = true;
int icon = R.drawable.ic_launcher;
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
String strMessage = loadPreferences();
String newMessage = "";
if (!strMessage.isEmpty())
newMessage = strMessage + "<br>" + msg;
else
newMessage = msg;
RemoteViews contentView = new RemoteViews(getPackageName(), R.layout.custom_notification_layout);
contentView.setImageViewResource(R.id.image, R.drawable.ic_launcher);
contentView.setTextViewText(R.id.text, Html.fromHtml(newMessage));
Notification notification = new Notification(icon, Html.fromHtml(msg), when);
notification.contentView = contentView;
String title = this.getString(R.string.app_name);
ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
List < ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);
Log.d("current task :", "CURRENT Activity ::" + taskInfo.get(0).topActivity.getClass().getSimpleName());
ComponentName componentInfo = taskInfo.get(0).topActivity;
Intent notificationIntent;
if(!componentInfo.getPackageName().equalsIgnoreCase("com.example.myapp")){
notificationIntent = new Intent(getApplicationContext(),
FirstActivity.class);
} else {
notificationIntent = new Intent(getApplicationContext(),
MainActivity.class);
notificationIntent.putExtra("login", true);
}
notificationIntent.putExtra("message", Html.fromHtml(msg));
oldMessage = newMessage;
savePreferences(getApplicationContext(), oldMessage);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
int notifyID = 1;
PendingIntent intent = PendingIntent.getActivity(this, notifyID,
notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(this, title, msg, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.contentIntent = intent;
notificationManager.notify(notifyID, notification);
}
When I send the message I receive it in device with Android 4.2 for example but not in devices with Android 2.3. I'm debugging the app with Android 2.3 and I have put a breakpoint in the method onHandleIntent of the IntentService but it never comes. I think that it could be for the errors that appears in logcat.
How can I solve the problem for devices with Android 2.3?
Thanks in advance.
I change Notification to NotificationCompat:
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);
mBuilder.setContentIntent(intent);
Notification notification = mBuilder.build();
notification.contentView = contentView;
notificationManager.notify(notifyID, notification);
but the problem persist, in logcat appears the same error
Try this code. I've used this in my project and it runs good. receiverActivity is activity that will open onclick, as I can remember, but I'm not 100% sure of it.
public static void showNotification ( Context context, Class receiverActivityClass, String title, String text, int icon )
{
Intent intent = new Intent( context, receiverActivityClass );
PendingIntent pendingIntent = PendingIntent.getActivity( context, 0, intent, 0 );
// Build notification
Notification notification = new Notification.Builder( context )
.setContentTitle( title )
.setContentText( text )
.setSmallIcon( icon )
.setContentIntent( pendingIntent )
.setAutoCancel( true )
.getNotification();
NotificationManager notificationManager =
( NotificationManager ) context.getSystemService( context.NOTIFICATION_SERVICE );
notificationManager.notify( 0, notification );
}
Related
I faced an issue with Notification within BroadcastReceiver().
as I know, My code worked properly before but it doesn't work now.
sometimes NotificationTicker appear but no title and content has been appeared.
here is my code. my searches couldn't help me to find where is the problem.
here is my CODE:
private void MyNotification(Context context) {
String NotificqationText = "NotificqationText";
String NotificationTitle = "NotificationTitle ";
String NotificationTicker = "NotificationTicker";
PendingIntent MyPendingIntent = PendingIntent.getActivity(context, 0, new Intent(context, Splash.class), 0);
NotificationCompat.Builder MyNB = new NotificationCompat.Builder(context);
MyNB.setSmallIcon(R.drawable.icon);
MyNB.setContentTitle(NotificationTitle);
MyNB.setContentText(NotificqationText);
MyNB.setTicker(NotificationTicker);
MyNB.setAutoCancel(true);
MyNB.setContentIntent(MyPendingIntent);
Bitmap MyPicture = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
MyNB.setLargeIcon(MyPicture);
NotificationCompat.BigPictureStyle MyPicStyle = new NotificationCompat.BigPictureStyle().bigPicture(MyPicture);
MyPicStyle.setSummaryText("Etude can makes our life Enlightened");
MyNB.setStyle(MyPicStyle);
MyNB.setStyle(new NotificationCompat.BigTextStyle());
NotificationCompat.BigTextStyle MyText = new NotificationCompat.BigTextStyle();
MyText.bigText(NotificqationText);
MyText.setBigContentTitle(NotificationTitle);
NotificationManager MyNotifyManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
MyNotifyManager.notify(1, MyNB.build());
}
I used Toast message to find my broadcastreceiver works or not and find broadcast works properly and only notification has problem
Try this code :
private void MyNotification(Context context) {
String NotificqationText = "NotificqationText";
String NotificationTitle = "NotificationTitle ";
String NotificationTicker = "NotificationTicker";
Intent intent = new Intent(this, Splash.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_CLEAR_TASK));
PendingIntent MyPendingIntent = PendingIntent.getActivity(this, 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
Bitmap MyPicture = BitmapFactory.decodeResource(context.getResources(), R.drawable.icon);
Notification MyNB = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon)
.setLargeIcon(MyPicture)
.setStyle()
.setBigContentTitle(NotificationTitle)
.setContentTitle(NotificationTitle)
.setContentText(NotificqationText)
.setTicker(NotificationTicker)
.setAutoCancel(true)
.setContentIntent(MyPendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
MyNB.flags |= Notification.FLAG_SHOW_LIGHTS;
MyNB.flags |= Notification.FLAG_AUTO_CANCEL;
MyNB.defaults = Notification.DEFAULT_ALL;
notificationManager.notify((int)System.currentTimeMillis(), MyNB);
}
i need to show a notification in a method from intent service, i modify the context to "getApplicationContext" but it doesnt show anything.
Im using the android example: http://developer.android.com/guide/topics/ui/notifiers/notifications.html
private void notf_SinConexion()
{
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder( getApplicationContext() )
.setSmallIcon( R.drawable.ic_action_action_alarm_on )
.setContentTitle( "Title" )
.setContentText( "Message" );
Intent resultIntent = new Intent( getApplicationContext(), MainActivity.class );
TaskStackBuilder stackBuilder = TaskStackBuilder.create( getApplicationContext() );
stackBuilder.addParentStack( MainActivity.class );
stackBuilder.addNextIntent( resultIntent );
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent( resultPendingIntent );
NotificationManager mNotificationManager =
(NotificationManager) getApplicationContext().getSystemService( Context.NOTIFICATION_SERVICE );
// mId allows you to update the notification later on.
mNotificationManager.notify( 10, mBuilder.build() );
}
First you need to read this article on how to use Notifications.
Next use this to send a Notification, you can write this code in the service class at the point where you receive some data from the client.
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
int icon = R.drawable.notification_icon;
CharSequence notiText = "Your notification from the service";
long meow = System.currentTimeMillis();
Notification notification = new Notification(icon, notiText, meow);
Context context = getApplicationContext();
CharSequence contentTitle = "Your notification";
CharSequence contentText = "Some data has arrived!";
Intent notificationIntent = new Intent(this, YourActivityThatYouWantToLaunch.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
int SERVER_DATA_RECEIVED = 1;
notificationManager.notify(SERVER_DATA_RECEIVED, notification);
I am creating a notification upon click I am asking it cancel a service. It just works fine below 4.4 (Kitkat). My app supports 2.2 (API 8 onwards)
On Kitkit (Nexus 5) The service isn't called at all. I am not where I am going wrong here? It just works fine even on version 4.3?
Here is what I have tried and working on every phone except Nexus 5(Kitkat)
createNotification("Click here to cancel notification!", "TestApp");
I am calling the above immediately after calling a particular service.
private void createNotification(String body,String title)
{
NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
int unique_id = 007;
Intent nintent = new Intent(this,ServicetocancelAlarm.class);
PendingIntent pin = PendingIntent.getService(this,0, nintent, 0);
//set notify image
Notification n = new Notification(R.drawable.ic_launcher, body,java.lang.System.currentTimeMillis());
n.contentIntent = pin;
n.setLatestEventInfo(this, title, body, pin);
n.defaults = Notification.DEFAULT_ALL;
nm.notify(unique_id, n);
}
Can somebody help me out fixing this issue with KITKAT?
Update:
I tried the following:
Notification("Message", "This is Android Notification Message");
And Method
#SuppressWarnings("deprecation")
private void Notification(String notificationTitle, String notificationMessage)
{
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
android.app.Notification notification = new android.app.Notification(R.drawable.ic_launcher, "A New Message",
System.currentTimeMillis());
Intent notificationIntent = new Intent(this, TransparentActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(MainActivity.this, notificationTitle, notificationMessage, pendingIntent);
notificationManager.notify(10001, notification);
}
This piece of code works fine on debug mode but if Export the apk and install it. It just doesn't work at all. It doesn't get to the new activity. I am not sure what is wrong here.
I solved the problem by trying the following code:
int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion >= 16)
{
Context context = RepeatService.this;
Intent notificationIntent = new Intent(context, ServicetocancelAlarm.class);
PendingIntent contentIntent = PendingIntent.getService(context, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Resources res = context.getResources();
Notification.Builder builder = new Notification.Builder(context);
builder.setContentIntent(contentIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setLargeIcon(BitmapFactory.decodeResource(res, R.drawable.ic_launcher))
.setTicker(res.getString(R.string.ticker))
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContentTitle(res.getString(R.string.app_name))
.setContentText(res.getString(R.string.cancelText));
Notification n = builder.build();
nm.notify(007, n);
}
Else part has the usual notification code that just works fine below API 16..
try it out!!
I am using the following code to create a Notification inside the onRecieve method of my BroadcastReceiver but it is giving me the following exception:
java.lang.IllegalArgumentException: contentIntent required
The code:
NotificationManager notificationManager;
notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
String tickerText;
String expandedText;
String expandedTitle;
int icon;
long when;
Notification notification;
int notificationref = new Random().nextInt(100) + 1;
icon = R.drawable.reminder;
tickerText = "New Reminder";
when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
expandedText = "Reminder at: "
+ DateOrTimeString.getTimeString(task.time) + "\n"
+ task.detail;
expandedTitle = "Reminder:" + task.topic;
Intent intentDestroyer = new Intent(context, RemindHomeActivity.class);
intentDestroyer.putExtra("ID", task.id);
intentDestroyer.putExtra("NOTIFICATIONREF", notificationref);
launchIntent = PendingIntent.getActivity(context, notificationref,
intentDestroyer, 0);
notification.setLatestEventInfo(mContext, expandedTitle, expandedText,
null);
notificationManager.notify(1, notification);
One more thing this problem is only in API level less than 11. it is wortking in API level 15 Icecream Sandwitch
You need to set the contentIntent
void android.app.Notification.setLatestEventInfo(Context context, CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent)
in your case:
notification.setLatestEventInfo(mContext, expandedTitle, expandedText,
launchIntent );
Hi i have my app aacplayer works good in my galaxy s3 but in a galaxy ace with android 2.3.4 my app crash but if i remove the notification manager from code then it works perfec.
heres my notificaction manager code:
public void playerMetadata(final String key, final String value) {
TextView tv = null;
if ("StreamTitle".equals(key) || "icy-name".equals(key)
|| "icy-description".equals(key)) {
tv = txtMetaTitle;
} else if ("icy-url".equals(key)) {
tv = txtMetaUrl;
} else
return;
final TextView ftv = tv;
uiHandler.post(new Runnable() {
public void run() {
ftv.setText(value);
int NOTIFICATION_ID = 1;
String ns = Context.NOTIFICATION_SERVICE;
Context context = getApplicationContext();
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.notificacion;
long when = System.currentTimeMillis();
CharSequence tickerText = "Reproduciendo...";
Notification notification = new Notification(icon, tickerText,
when);
RemoteViews contentView = new RemoteViews(getPackageName(),
R.layout.notification);
contentView.setImageViewResource(R.id.icon, R.drawable.notificacion);
contentView.setTextViewText(R.id.title_text, txtMetaTitle
.getText().toString());
contentView.setTextViewText(R.id.message_text, txtMetaUrl
.getText().toString());
notification.contentView = contentView;
Intent notificationIntent = new Intent(AACPlayerActivity.this,
Principal.class);
// Intent notificationIntent = new Intent(this,
// Principal.class);
PendingIntent contentIntent = PendingIntent.getActivity(
context, 0, notificationIntent, 0);
// PendingIntent contentIntent = PendingIntent.getActivity(this,
// 0, notificationIntent, 0);
notification.contentIntent = contentIntent;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
mNotificationManager.notify(NOTIFICATION_ID, notification);
}
});
}
i dont know why works good in the galaxy s3 but not in the galaxy ace how i can fix that.
thank you very much.
edit:
logcat:
E/AndroidRuntime(16846): android.app.RemoteServiceException: Bad notification posted from package com.spoledge.aacplayer: Couldn't expand RemoteViews for: StatusBarNotification(package=com.spoledge.aacplayer id=1 tag=null notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x2))
if i add this in my code:
notification.setLatestEventInfo(context,
txtMetaTitle.getText().toString(),
txtMetaUrl.getText().toString(), contentIntent);
notification works good no issues, but i need to show the notification with this:
contentView.setTextViewText(R.id.title_text, txtMetaTitle
.getText().toString());
contentView.setTextViewText(R.id.message_text, txtMetaUrl
.getText().toString());
that txtmetatitle field have a marquee but if add that line posted before notification.setLatestEvent, works without crash but i cannot make the marquee run correctly