Ignoring notification with - android

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.

Related

AlarmManager + NotificationManager not working

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"

Intent Filter Pending Intent , Broadcast Reciever and Notification Manager

I'm working hard to understand these concepts how they are working.
can someone explain these concepts to me??
here is the code I want to understand
Intent intent = new Intent();
intent.setAction("com.example.akshay.proximityalertexample2");
PendingIntent intent1 = PendingIntent.getBroadcast(this, 0, intent, 0);
locationManager.addProximityAlert(LAT, LONG, 200, -1, intent1);
IntentFilter filter = new IntentFilter("com.example.akshay.proximityalertexample2");
registerReceiver(new ProximityAlert(), filter);
Broadcast class file
package com.example.akshay.proximityalertexample2;
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.location.LocationManager;
import android.util.Log;
import android.widget.Toast;
/**
* Created by Akshay on 9/18/2015.
*/
public class ProximityAlert extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String key = LocationManager.KEY_PROXIMITY_ENTERING;
Toast.makeText(context, key, Toast.LENGTH_SHORT).show();
Boolean entering = intent.getBooleanExtra(key, false);
if (entering) {
Log.e(getClass().getSimpleName(), "entering");
} else {
Log.e(getClass().getSimpleName(), "exiting");
}
NotificationManager notificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
Notification notification = createNotification();
PendingIntent pendingIntent = PendingIntent.getActivity(context , 0 , null , 0 );
notification.setLatestEventInfo(context,"Proximity Alert!!","You Are Near the Point of intereste " ,pendingIntent);
}
private Notification createNotification() {
Notification notification = new Notification();
notification.when = System.currentTimeMillis();
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
notification.ledOffMS = 1500;
notification.ledOnMS = 1500;
return notification;
}
}
Please explain me the working of these concepts..
any help would mean me alot
thanks a lot
This is a really old thread, but I figured for my own learning and the benefit of anyone else arriving on this thread I'd take a shot at it.
In the first snippet, you are creating an intent that doesn't do anything, and wrapping it with a pendingIntent, to be called by the app later at some point. The intent filter serves to let other apps (mainly the receiver in this case) know what kind of intent are we dealing with (it's a poor example, would've personally gone with something like PROX_ALERT_INTENT). We add a proximity alert, and register the intent with our Receiver class.
In the Receiver class, We first define the boolean key that determines if we've stepped in the proximity alert radius or not. Then we fetch the key from the broadcasted intent (its passed automatically), following which we check if the user has indeed entered the proximity alert area.
If he has, we create a notification that has a pending intent attached (which in this case does nothing but could launch a new activity for example), and launch the notification.
Note that this method of notification building has deprecated, have to use the builder now.

how to show android notification and application icon

I have a service running, and would like to send a notification. Too bad, the notification object requires a context, like an Activity, and not a service.
Do you know any way to by pass that ? I tried to create an Activity for each notification bu it seems ugly, and I can't find a way to launch an Activity without any view.
i also want to sent my application icon to notification to show icon top of screen
Here is a working code , which creates a notification from a service itself.
hope it will help you,
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
#Override
public IBinder onBind(Intent arg0) {
return null;
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
//We get a reference to the NotificationManager
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
String MyText = "Reminder";
Notification mNotification = new Notification(R.drawable.ic_launcher, MyText, System.currentTimeMillis() );
//The three parameters are: 1. an icon, 2. a title, 3. time when the notification appears
String MyNotificationTitle = "Medicine!";
String MyNotificationText = "Don't forget to take your medicine!";
Intent MyIntent = new Intent(Intent.ACTION_VIEW);
PendingIntent StartIntent = PendingIntent.getActivity(getApplicationContext(),0,MyIntent, PendingIntent.FLAG_CANCEL_CURRENT);
//A PendingIntent will be fired when the notification is clicked. The FLAG_CANCEL_CURRENT flag cancels the pendingintent
mNotification.setLatestEventInfo(getApplicationContext(), MyNotificationTitle, MyNotificationText, StartIntent);
int NOTIFICATION_ID = 1;
notificationManager.notify(NOTIFICATION_ID , mNotification);
//We are passing the notification to the NotificationManager with a unique id.
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
return START_STICKY;
}
#Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
}
}

Android Push Notification Message Cut off

Hi im working on a project that uses push notifications to push out information in a text format. Only when i receive the push notification the text is cut off and replaced with "..."
Im using the NotificationCompaq builder to build my notifications when the app receives the information from the receiver.
Here is my GCMBroadcastReceiver Im pretty sure the issue is in the Builder but Im not sure what i need to do to fix it.
import utilities.CommonUtilities;
import android.app.Activity;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.gcm.GoogleCloudMessaging;
/**
* Handles the incoming messages from the server.
*
*/
public class GCMBroadcastReceiver extends BroadcastReceiver {
static final String TAG = "GCMDemo";
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
Context ctx;
#Override
public void onReceive(Context context, Intent intent) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
ctx = context;
String messageType = gcm.getMessageType(intent);
String message = intent.getExtras().getString("price");
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
sendNotification("Send error: " + intent.getExtras().toString());
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
sendNotification("Deleted messages on server: " +
intent.getExtras().toString());
} else {
sendNotification(message);
}
setResultCode(Activity.RESULT_OK);
}
// Put the GCM message into a notification and post it.
private void sendNotification(String msg) {
mNotificationManager = (NotificationManager)
ctx.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0,
new Intent(ctx, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(ctx)
.setSmallIcon(R.drawable.icon)
.setContentTitle(CommonUtilities.pushTitle)
.setStyle(new NotificationCompat.BigTextStyle())
.setContentText(msg)
.setLights(0xff00ff00, 300, 1000)
.setVibrate(CommonUtilities.pattern);
Log.d(TAG, "Notification built " + mBuilder.toString());
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Toast.makeText(ctx, "New Message: " + msg, Toast.LENGTH_LONG).show();
}
}
Any help on this would be greatly appreciated thanks.
Edit
Ive checked the Message received from the GCM before my app builds the notification and i am receiving the full message there is nothing being cut off during the receiving of the message so it is definitely something to do with the Notification builder. I Just cannot seem to figure out whats causing it to do that.

Wrong timestamp in Android Notification

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);
}
}

Categories

Resources