This question already has an answer here:
Android show a notification not working
(1 answer)
Closed 6 years ago.
I am sending a notificatioin from this Broadcast reciver which is triiggered by an alarm manager to send sms but i cant see any notification.
I also tried to include sent intent and delivery intent but cant use registerReciever here.
Here is my Code.
Broadcast Reciever
public class MyReceiver extends BroadcastReceiver {
String SENT="sent";
public final String tag="com.example.pritesh.smstimer";
public MyReceiver() {
}
//#Override
public void onReceive(Context context, Intent intent) {
Log.i(tag,"Sending");
NotificationManager notificationManager= (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1=new Intent(context,Time_Picker.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent1,0);
Notification.Builder notification=new Notification.Builder(context)
.setContentTitle("SMS SENT")
.setContentText("Your SMS has Been Sent")
.setContentIntent(pendingIntent);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(Time_Picker.Phone,null, Time_Picker.Message, null, null);
notificationManager.notify(0,notification.build());
Toast.makeText(context, "Sms Sent", Toast.LENGTH_LONG).show();
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/favicon"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Time_Picker"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".noti"/>
<activity android:name=".MainActivity">
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"/>
</application>
Have you registered the receiver in your androidmanifest.xml or called registerReceiver to do the same? I don't think it will work until you do.
See here:https://developer.android.com/guide/topics/manifest/receiver-element.html
Related
I wrote my custom broadcast receiver to receive intents from my other app but it doesn't recieve anything. Im sure that first app is sending broadcast corectly. Can someone help me?
App1:
public void broadcastIntent() {
Intent intent = new Intent();
String permissions = "com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION";
intent.putExtra("name", editName.getText().toString());
intent.putExtra("price", Float.parseFloat(editPrice.getText().toString()));
intent.putExtra("quantity", Integer.parseInt(editQuantity.getText().toString()));
intent.setAction("com.example.android.projekt1.notification");
sendBroadcast(intent, permissions);
}
And I run this function on my setOnClickListener method.
There is my broadcast receiver:
public class MyBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Intennt received.", Toast.LENGTH_LONG).show();
Intent serviceIntent = new Intent(context, MyService.class);
serviceIntent.putExtras(intent);
context.startService(serviceIntent);
}
}
So while sending broadcast I have 0 toast messages and my service doesn't run too.
There is androidManifest from receiver:
<?xml version="1.0" encoding="utf-8"?>
<permission-group android:name="com.example.android.mybroadcastreceiver.my_permissions"
android:label="my permissions group"/>
<permission android:name="com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION"
android:permissionGroup="com.example.android.mybroadcastreceiver.my_permissions"
android:label="my permission"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadcastReceiver"
android:permission="com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION">
<intent-filter>
<action android:name="com.example.android.projekt1.notification">
</action>
</intent-filter>
</receiver>
<service android:name="MyService" />
</application>
I run my receiver first then I run my main app and press the button to send broadcast
set package name of targeting application:
public void broadcastIntent() {
Intent intent = new Intent();
String permissions = "com.example.android.mybroadcastreceiver.my_permissions.MY_PERMISSION";
intent.putExtra("name", editName.getText().toString());
intent.putExtra("price", Float.parseFloat(editPrice.getText().toString()));
intent.putExtra("quantity", Integer.parseInt(editQuantity.getText().toString()));
intent.setAction("com.example.android.projekt1.notification");
intent.setPackage("Package name of receiver app");// set Package of targeting app
sendBroadcast(intent, permissions);
}
I created a notification setup in my application.when the user clicks the notification an activity Coding is suppose to open.But it isn't.When i checked the phone log(in the console of the android studio) it has some thing like this in it:
10-19 19:18:14.598 888-1437/? W/ActivityManager: Permission Denial: starting Intent { flg=0x1000c000 cmp=com.defcomdevs.invento16/.Coding bnds=[0,874][1080,1060] } from null (pid=-1, uid=10169) not exported from uid 10185
i don't understand what that is?
my code for notification is:
public class AlarmReceiver extends BroadcastReceiver {
static int notifyId=1;
#Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context,"Alarm has been set",Toast.LENGTH_SHORT).show();
NotificationCompat.Builder mNotify=new NotificationCompat.Builder(context);
mNotify.setSmallIcon(R.drawable.index);
mNotify.setContentTitle("Coding");
mNotify.setContentText("INVENTO: Coding competition is going to be conducted today.");
Intent resultIntent=new Intent(context,Coding.class); //activity to open up when user clicks the notification
TaskStackBuilder stackBuilder=TaskStackBuilder.create(context);
stackBuilder.addParentStack(Coding.class); //add the to-be-displayed activity to the top of stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mNotify.setContentIntent(resultPendingIntent);
NotificationManager notificationManager=(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notifyId, mNotify.build());
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
//note: on click display activity is not working.
}
}
please help!!
My manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.defcomdevs.invento16" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AlarmActivity"
android:label="#string/title_activity_alarm"
android:theme="#style/AppTheme.NoActionBar" >
</activity>
<receiver
android:name=".AlarmReceiver"
android:process=":remote" />
<activity
android:name=".Registration"
android:label="#string/title_activity_registration"
android:theme="#style/AppTheme.NoActionBar" >
</activity>
<activity
android:name=".Coding"
android:label="#string/title_activity_coding"
android:theme="#style/AppTheme.NoActionBar" >
</activity>
</application>
Add android:exported="true" to your .Coding activity tag in the manifest.xmlfile. Though have in mind that this allows other applications to start your activity.
I am trying to use alarm manager to show notification after 5 sec. I have tried many sites but was not able to understand please give a simple example for explaining how to use alarm manager and connect notification with it.
I am newbie.
this is function i used to set alarm and I am not getting notification after 5 secs actually not at all not in emulator nor in android mobile. If put code to create notififcation with a button pressing that is working great.
public void setAlarm(View view)
{
Intent alertIntent = new Intent(this, AlertReciver.class);
Long alertTime = new GregorianCalendar().getTimeInMillis()+5*1000;
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alertTime, PendingIntent.getBroadcast(this, 1, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
tv.setText("completed");
}
and this class to make it work
public class AlertReciver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
createNotification(context,"times Up", "5 SEcond has passed", "Alert");
}
public void createNotification(Context context,String msg, String msgText, String msgAlert){
PendingIntent notificIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ne)
.setContentTitle(msg)
.setTicker(msgAlert)
.setContentText(msgText);
mBuilder.setContentIntent(notificIntent);
mBuilder.setDefaults(NotificationCompat.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
this is the manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.deepaksingh.goinnotifying" >
<uses-permission android:name="android.permission.alarm.permission.SET_ALARM" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MoreInfoNotification"
android:label="#string/title_activity_more_info_notification" >
<meta-data
android:name="android support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</application>
You havn't set permission for wake:
<uses-permission android:name="android.permission.WAKE_LOCK" />
ANd you didn't register your recevier.Here.
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MoreInfoNotification"
android:label="#string/title_activity_more_info_notification" >
<meta-data
android:name="android support.PARENT_ACTIVITY"
android:value=".MainActivity" />
<receiver android:name=".AlertReciver "/>
</application>
The purpose of my app is to send an email at a predetermined time based on a schedule set with a broadcast receiver alarm using the users built in email configuration. The To: and Subject are already filled out. All the user has to do, is click on the notification when the alarm goes off, and it should prompt the user via an intent what email program to use via my SendMail.class.
If I call the class directly by dropping it into my "MainActivity onCreate it works. If I place it in my Intent definition where I build my notification it never appears to get called. This is the meat of my problem. I need the intent create chooser to pop up with its list of valid email programs when the user clicks on the notification and its just not working. Any help would be greatly appreciated! :)
Coincidentally, I also have a preferences fragment to hold the settings of my app, and if I replace the call to SendMail.class to SetPreferenceActivity, then the notification works as expected (i.e. it successfully calls another class when the user clicks on it).
Here is the code for my notification manager method:
public void createNotification(View view) {
Context context = getApplicationContext();
SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String default_subject = mySharedPreferences.getString("default_subject", "");
default_subject = default_subject + android.text.format.DateFormat.format("E MM-dd", new java.util.Date());
Intent intent = new Intent(this, SendMail.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Notification noti = new Notification.Builder(this)
.setContentTitle("Time to Send the Email")
.setContentText("Date: "+default_subject)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
And here is what is going on in the SendMail.class
public class SendMail extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendMail();
}
public void sendMail() {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", "somebody#gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Some Subject Text");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
}
}
Here is the contents of my Manifest
<.....snipped header>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SetPreferenceActivity"
android:label="#string/app_name" >
</activity>
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
android:label="MintBootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED" >
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</action>
</intent-filter>
</receiver>
</application>
I have created a notification in my application to show the battery level in percentage. My manifest file is
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.create.bg"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.BATTERY_STATS"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".CreateBgActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".BatteryReceive" >
<intent-filter>
<action android:name="android.intent.action.BATTERY_CHANGED"></action>
</intent-filter>
</receiver>
</application>
I created a separate class that extends BroadcastReceiver to receive the battery level and notify in the status bar. My code is:
public class BatteryReceive extends BroadcastReceiver {
private String bat = "android.intent.action.BATTERY_CHANGED";
#Override
public void onReceive(Context context, Intent intent) {
//Log.d("Battery Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1));
if(intent.getAction().equals(bat)) {
Toast.makeText(context, "Battery Level"+intent.getIntExtra("level", 0), Toast.LENGTH_SHORT).show();
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
final Notification notifyDetails = new Notification(R.drawable.ic_launcher,""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1),System.currentTimeMillis());
//notifyDetails.flags = Notification.FLAG_AUTO_CANCEL;
notifyDetails.setLatestEventInfo(context, "Batter Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1), null);
mNotificationManager.notify(1, notifyDetails);
}
}
}
I could't get any notification or Log output for the battery level :( Is it anything wrong with my code. SOme one help me out of this.
#Override
public void onReceive(Context context, Intent intent) {
//Log.d("Battery Level", ""+intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1));
if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED)) {
// This is called when Your battery is changed
}
}
Change:
if(intent.getAction().equals(bat))
To:
if(intent.getAction().equals(Intent.ACTION_BATTERY_CHANGED))