So i was working out on Android notifications,
I tried out one the examples of notification on android documentation:
In one of the examples it shows, how to open an activity from notification.
So the Problem is that: When i try to press back button on the activity opened by notification it should go back to MainActivity but the app closes out.
ResultActivity is just the default empty Activity with a TextView
MainActivity.java:
// The id of the channel.
String CHANNEL_ID = "my_channel_01";
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(android.R.drawable.stat_notify_more)
.setContentTitle("Event tracker")
.setContentText("Events received");
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = new String[6];
// / Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
events[i] = "Event " + i;
inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inboxStyle);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="justjava.andriod.example.com.notificationchannel">
<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>
<activity android:name=".ResultActivity"
android:parentActivityName=".MainActivity"
/>
</application>
</manifest>
Replace the line:
stackBuilder.addParentStack(MainActivity.class);
With:
stackBuilder.addParentStack(ResultActivity.class);
Also add the <meta-data tag to your activity in the manifest if you're testing on API level less than 16
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
Related
I have some trouble ring an alarm on android-studio app. First, I have a method, called with a button ( onClick), then I create alarmManager in this, and try to call a class AlarmReceiver to ring the alarm. The problem is that the program running never go in my AlarmReceiver class, and I don't understand why. Let me show you the code:
public void sendMessage(View v) {
long time = currentTimeMillis();
Toast.makeText(this, "ALARM ON", Toast.LENGTH_SHORT).show();
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
calendar.set(Calendar.MINUTE, 58);
Toast.makeText(MainActivity.this, "bjr", Toast.LENGTH_SHORT).show();
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);}
This is the method called when click on button, and now this is the AlarmReceiver:
public class AlarmReceiver extends WakefulBroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
Toast.makeText(context, "Alarm! Wake up! Wake up!", Toast.LENGTH_LONG).show();
Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if (alarmUri == null)
{
alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
}
Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);
ringtone.play();
}
}
So, when I run the app, then click on button, the word " ALARM ON" and "bjr" is printed, but the text so in AlarmReceiver "Alarm! Wake up!" Is never printed, so the app never go in this class, I need to know why.
My android manifest :
`<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.beantunes.myapplication">
<uses-feature
android:name="android.hardware.sensor.accelerometer"
android:required="true"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".vue.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".vue.AlarmReceiver"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
<activity android:name=".vue.DisplayMessageActivity"></activity>
</application>
`
I'm having some trouble adding notifications to my app. I would like a notification everyday at 10am, for example. Here's my code:
MainActivity.java
public class MainActivity extends AppCompatActivity {
private PendingIntent alarmIntent;
private AlarmManager alarmMgr;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAlarm();
}
private void setAlarm() {
alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 10);
calendar.set(Calendar.MINUTE, 00);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 3, alarmIntent);
}
AlarmReceiver.java
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_stat_name)
.setContentTitle("My notification")
.setContentText("Hello World!");
Intent resultIntent = new Intent(context, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, mBuilder.build());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.turnu_a.todolist">
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<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=".taskView.newTaskActivity" />
<activity android:name=".taskView.showTaskDetailsActivity" />
<activity android:name=".taskView.updateTaskActivity" />
<activity android:name=".settings.settingsActivity"></activity>
<receiver android:name=".Alarm.AlarmReceiver"></receiver>
</application>
The thing is, even though I change the time for the trigger to happen in a minute or two (in order to test my code), absolutely nothing happens. I've tried to create a notification in the "setAlarm()" method and it worked, meaning the problem doesn't come from the creation of the notification but the alarmManager not being triggered.
I'd appreciate any help. Thanks.
So... I removed this line:
calendar.setTimeInMillis(System.currentTimeMillis());
from the "setAlarm()" method and now it works.
I am glad it works but a bit surprised the problem came from here (though in hindsight, I don't think this line is quite useful anyway).
Anyway, problem solved, thanks to everyone who tried to help.
hi i have used the following code in receiver class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("the time is right","yay!");
Intent i = new Intent(context, AlarmServie.class);
context.startService(i);
}
}
here i the code which i used in service class
public class AlarmServie extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e("onStart:","came" );
/* NotificationManager notifyman = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent main_activity = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent o = PendingIntent.getActivity(this, 0, main_activity, 0);
/*Notification noti = new Notification.Builder(this)
.setContentTitle("Reminder to Pill")
.setContentText("Click for info")
.setAutoCancel(true)
.setContentIntent(o)
.build();*/
NotificationManager nm = (NotificationManager) this.getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
Intent in = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent pending = PendingIntent.getactivity(this, 0, in, 0);
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(AlarmServie.this);
mBuilder.setContentTitle("Pill Reminder");
mBuilder.setContentText("CLick here to View");
//mBuilder.setSound(sound);
TaskStackBuilder ts=TaskStackBuilder.create(this);
ts.addParentStack(MainActivity.class);
nm.notify(9999,mBuilder.build());
}}
i created this code to get notification.when i run the app the receiver class is triggered but it is not moving to service class to invoke notification.can anyone say whats wrong in the code or say me how to get notification using broadcast receiver and service in a detailed tutorial
here is the manifest file and say me where to put the code exactly
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<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>
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".Set"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
please explain in detail what the problem in code.please tell exactly where to put the service tag
You must declare service in manifest also.
Intent to start service is sent to system then system tries to find right application component to handle this particular intent using information passed in manifest files.
It should be enough, but I'm not sure if you put service in the default package.
<application>
....
<service android:name=".AlarmServie"/>
....
</application>
Your Manifest should look something like this
<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>
//here is your service
<service android:name=".AlarmServie" android:enabled="true"/>
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".Set"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
I have different Ongoing notifications. When an addAction() button is pressed a BroadcastReceiver is called and my notification is cleaned. The problem is the addAction() always pass the id of the last notification added, not the one that you pressed:
Here is the method adding the notification:
public void pushNotification(View v){
String text = editText.getText().toString().trim();
editText.setText("");
int millis = (int) (System.currentTimeMillis() % Integer.MAX_VALUE);
Intent notificationIntent = new Intent(mContext, NotificationReceiver.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
notificationIntent.putExtra("id", millis);
PendingIntent pIntent = PendingIntent.getBroadcast(mContext, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder notification = new NotificationCompat.Builder(mContext);
notification.setContentTitle("");
notification.setContentText(text);
notification.setSmallIcon(R.mipmap.ic_launcher);
notification.setOngoing(true);
notification.addAction(R.mipmap.ic_launcher, "Dismiss", pIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(millis, notification.build());
}
Receiver:
public class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.cancel(intent.getIntExtra("id", 0));
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="PACKAGE_NAME">
<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>
<receiver android:name=".NotificationReceiver" />
</application>
I found the solution, just add FLAG_ONE_SHOT to your pending intent and now all the extras will be different.
I am trying to send a push notification on a button click event. But don't know why, but it is not working. It doesn't show any error though. Can anybody find any error in the code?
public class Verify extends AppCompatActivity {
public static String TAG=Verify.class.getSimpleName();
NotificationCompat.Builder notification;
private static final int uniqueID=12345;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_verify);
notification=new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
}
public void onVerify(View v)
{
this.defineNotification();
//other code follows
}
public void defineNotification()
{
notification.setContentTitle("Successfully Signed Up");
notification.setContentText("Hi, you just Signed Up as a Vendor");
notification.setWhen(System.currentTimeMillis());
Intent intent=new Intent(this,OtherActivity.class);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
NotificationManager nm=(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nm.notify(uniqueID,notification.build());
//Log.i(TAG, "coming here in notification");
}
Here is the Android Manifest Code :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sachinparashar.xyz">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".SignUp"
android:label="SignUp"
android:theme="#style/AppTheme"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Verify"
android:label="#string/title_activity_verify"
android:theme="#style/AppTheme"/>
<activity
android:name=".VendorDetails"
android:label="Vendor Details"
android:theme="#style/AppTheme"/>
<activity
android:name=".OrderTypes"
android:label="Orders"
android:theme="#style/AppTheme" />
</application>
change this :
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
with this :
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
Insted of
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);
use
PendingIntent pendingIntent=PendingIntent.getActivity(Verify.this,0,intent,0);