I have been searching a lot of questions and answers in stackoverflow. I am still not able to solve my problem.
I set broadcast for a particular time.
I receive the broadcast perfectly in another class 'AlarmReceiver.java' which creates notification on receiving broadcast.
But I pass a string through an intent that calls a class 'AlarmResponder.java' on pressing the notification.
The class is getting called.
I receive the String data i passed through the intent only for the first time.
Next time I dont get anything. getextras() gives me null .
If i change the ID of the notification in NotificationManager.notify(1, mBuilder.build()); as
NotificationManager.notify(2, mBuilder.build()); it works .
But always it works only once
Please help me
public class AlarmReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
CharSequence reminderText = intent.getCharSequenceExtra("reminderText");
if (reminderText != null && reminderText != "") {
Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
Ringtone r = RingtoneManager.getRingtone(context, notification);
r.play();
//Toast.makeText(context, intent.getCharSequenceExtra("reminderText"), Toast.LENGTH_LONG).show();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(reminderText)
.setContentText("its time buddy");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(context, AlarmResponder.class);
resultIntent.putExtra("reminderText", reminderText);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(AlarmResponder.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
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(1, mBuilder.build());
}
}
}
and the AlarmResponder class is
public class AlarmResponder extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
onNewIntent(getIntent());
}
#Override
public void onNewIntent(Intent intent){
Bundle extras = intent.getExtras();
if(extras != null){
if(extras.containsKey("reminderText"))
{
setContentView(R.layout.alarm_responder);
TextView reminderText = (TextView) findViewById(R.id.reminderText);
// extract the extra-data in the Notification
String msg = extras.getString("reminderText");
reminderText.setText(msg);
}
}
}
}
Related
I have MainActivity and UserAcitivity, I have passed some values to UserActivity through intent.
from the UserActivity, I call Notification then notification appeared, I made notification tap to
start the UserActivity by Pending Intent. When the Notification tap the App crashes because the values passed from MainAcitvity become null.
I can't Use SharedPreferences because the Value conflict for another UserAcitivity(the passed value was unique for every user)
example code is here:-
This is MainAcitvity
public void buttonClick(View view){
Intent intent = new Intent(this,SecondActivity.class);
intent.putExtra("myname","ashiqueHira");
startActivity(intent);
}
This is SecondAcivity Oncreate method;
String heading= "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
heading = intent.getStringExtra("myname");
Toast.makeText(this, heading, Toast.LENGTH_SHORT).show();
}
This is my Notification method
public void notificationPops(){
Intent intent = new Intent(this, SecondActivity.class);
createNotificationChannel();
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(intent);
Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_vibration_black_24dp)
.setContentTitle("Token Reminder")
.setContentText("Dismiss the Alarm by Clicking this")
.setPriority(NotificationCompat.PRIORITY_MAX)
.setVibrate(new long[] { 100, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000, 1000 })
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
// Set the intent that will fire when the user taps the notification
.setContentIntent(pendingIntent)
.setAutoCancel(true);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, builder.build());
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"myChannel",
NotificationManager.IMPORTANCE_HIGH
);
serviceChannel.enableVibration(true);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
This is a similar condition that I have. The difference is I have many usages for the getIntent value
and that value is unique for every Activity.
Thanks in Advance
So, basically you get NullPointerException. For reducing this problem change youe SecondActivity code like this:
String heading= "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent = getIntent();
if(intent.getStringExtra("myname")!= null){
heading = intent.getStringExtra("myname");
Toast.makeText(this, heading, Toast.LENGTH_SHORT).show();
}
}
This will reduce your nullException error and your app cannot crash.
Another thing If you always need heading value pass through intent. Then pass the value also from notification pendingIntent and get it from SecondActivity
I found a way to solve this problem by adding and replacing the following code
in the notificationPops() method
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("yourName",heading);
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
this will get you the value to the activity when the activity stats by notification taps.
Please post if other best practices to solve the problem above
My app repeatedly loads data from a server. Among those data are messages.
Whenever new messages are loaded, the MainActivity is called on an Interface's callback method onMessagesReceived(int numOfMessages).
The app has only one Activity and every screen is implemented as a Fragment. Switching of Fragments is managed by a dedicated Navigator class.
My problem is the handling of the user tapping on the Notification. When the user taps on the Notification, the message list should be shown.
public class MainActivity extends AppCompatActivity implements MessageListener {
private static final int MESSAGE_NOTIFICATION_ID = 101010;
private static final String EXTRA_SHOW_MESSAGES = "SHOW_MESSAGES";
private Navigator mNavigator;
#Override
onMessagesReceived(int numOfMessages) {
Intent intent = new Intent(this, MainActivity.class);
testIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
testIntent.putExtra(EXTRA_SHOW_MESSAGES, true);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "testChannel")
.setSmallIcon(R.drawable.ic_message_notification)
.setContentTitle("New messages!")
.setContentText("You got " + numOfMessages + " new messages")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(MESSAGE_NOTIFICATION_ID, builder.build());
}
#Override
protected void onResume() {
super.onResume();
Bundle extras = getIntent().getExtras();
if (extras != null && extras.containsKey(EXTRA_SHOW_MESSAGES)) {
if (extras.getBoolean(EXTRA_SHOW_MESSAGES)) {
mNavigator.openMessageList();
}
}
}
}
At the moment, the MainActivity shows up, when the app is in background, but in onResume, the Bundle returns as null.
When the app is in the foreground, nothing happens at all.
I want to achieve on a click on the Notification:
- When the user is inside the app, the MessageList Fragment should be opened
- When the user is not inside the app, it should be started before opening the MessageList Fragment
Can someone give me a hint, how to proceed from here? Maybe using Intents isn't the right solution here?
You can use intents just put some boolean extras in intent an just check the value in main activity if that extra value is true then call your method.
After some more digging on Intents and Notifications, I finally came up with a solution.
public class MainActivity extends AppCompatActivity implements MessageListener {
private static final int MESSAGE_NOTIFICATION_ID = 101010;
private static final String EXTRA_SHOW_MESSAGES = "SHOW_MESSAGES";
private Navigator mNavigator;
#Override
onMessagesReceived(int numOfMessages) {
Intent intent = new Intent(this, MainActivity.class);
testIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
testIntent.putExtra(EXTRA_SHOW_MESSAGES, true);
PendingIntent pendingIntent = PendingIntent.getActivity(
this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "testChannel")
.setSmallIcon(R.drawable.ic_message_notification)
.setContentTitle("New messages!")
.setContentText("You got " + numOfMessages + " new messages")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(MESSAGE_NOTIFICATION_ID, builder.build());
}
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
this.setIntent(intetnt)
Bundle extras = intent.getExtras();
if (extras != null && extras.containsKey(EXTRA_SHOW_MESSAGES)) {
if (extras.getBoolean(EXTRA_SHOW_MESSAGES)) {
mNavigator.openMessageList();
}
}
}
}
I moved my code, reading the new Intent, to onNewIntent. This method gets called, when an Activity gets a new Intent and before onResume. This triggers regardless of the Activity being in the foreground or not. I also set this new Intent to be the Activities Intent with setIntent, otherwise the Intent that initialy started my Activity, is called by getIntent().
I have try all the methods but it doesn't work for me. i want to open or resume app with whatever screen open while click on notification.
I used following method:
NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
notiStyle.setBigContentTitle(team);
notiStyle.bigText(message);
Intent resultIntent = new Intent(this, MainDrawerActivity.class);
resultIntent.putExtra("fromNotification", "notification");
resultIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
int icon = R.mipmap.ic_launcher;
return new NotificationCompat.Builder(this).setSmallIcon(icon)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent).setContentTitle(team)
.setContentText(message).setStyle(notiStyle).build();
To raise your application to the foreground without starting any new activity, fire its launcher intent.
This method is from an old project of mine.
/**
* Creates a new launcher intent, equivalent to the intent generated by
* clicking the icon on the home screen.
*
* #return the launcher intent
*/
public static Intent newLauncherIntent(final Context context) {
final Intent intent = new Intent(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
return intent;
}
The intent created by this method does not start a new task if the app is running, even though it has that flag.
This is another way to obtain a launcher intent. However, I found that this intent would always start a new task, which is not what you want if the app is running.
final Intent intent = context.getPackageManager()
.getLaunchIntentForPackage(BuildConfig.APPLICATION_ID);
This is working fine for following three conditions:
1.if app already open and click on notification, notification should remove from status bar.
2.if app is open and in background then app should resume with whatever screen open already previously.
3.if app is close and click on notification in status bar then app should open.
private final static int NORMAL = 0x00;
private final static int BIG_TEXT_STYLE = 0x01;
private static NotificationManager mNotificationManager;
in onMessage call
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
new CreateNotification(BIG_TEXT_STYLE, team, message).execute();
then declare following class in GCMIntentService.
public class CreateNotification extends AsyncTask {
int style = NORMAL;
String team, message;
public CreateNotification(int style, String team, String message) {
this.style = style;
this.team = team;
this.message = message;
}
#Override
protected Void doInBackground(Void... params) {
Notification noti = new Notification();
switch (style) {
case BIG_TEXT_STYLE:
noti = setBigTextStyleNotification(team, message);
break;
}
noti.sound = (null);
noti.defaults = 0;
noti.sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.beep);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(0, noti);
return null;
}
}
and finally
private Notification setBigTextStyleNotification(String team, String message) {
// Create the style object with BigTextStyle subclass.
NotificationCompat.BigTextStyle notiStyle = new NotificationCompat.BigTextStyle();
notiStyle.setBigContentTitle(team);
notiStyle.bigText(message);
Intent resultIntent = getPackageManager()
.getLaunchIntentForPackage(getPackageName());
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// 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);
int icon = R.mipmap.ic_launcher;
return new NotificationCompat.Builder(this).setSmallIcon(icon)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent).setContentTitle(team)
.setContentText(message).setStyle(notiStyle).build();
}
You should have something like this in Application class to store the current activity.
private BaseActivity mCurrentActivity = null;
public BaseActivity getCurrentActivity() {
return mCurrentActivity;
}
public void setCurrentActivity(BaseActivity currentActivity) {
this.mCurrentActivity = currentActivity;
}
Then, inside your handle notification Service class.
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
BaseActivity currentActivity = ((App) this.getApplicationContext())
.getCurrentActivity();
Intent intent;
if (currentActivity instanceof ActivityA) {
intent = new Intent(this, ActivityA.class);
} else if (currentActivity instanceof ActivityB) {
intent = new Intent(this, ActivityB.class);
} else {
intent = new Intent(this, MainActivity.class);
}
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// your code...
}
If your app is killed, default activity will be called, such as MainActivity.
Else, when you receive & click push notification message when app is on foreground or background. Current activity will stay there as default activity, such as ActivityA & ActivityB. Then you can navigate to wherever another activities or fragments.
My suggestion, better we should use Fragment, it's easier in navigate to specially screen from push notification.
//I am using write now this can possible
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
Intent notificationIntent = new Intent(context, HomeActivity.class);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);
If you want to just resume the app state then instead of multiple activity I will suggest you just keep single activity and use Fragments for different screen.
On Notification click you need to define entry point of app in notification payload and the entry point decide what will be the next navigation.
If you are having only single activity then you can define that activity as a entry point and on the activity you can decide do you have to push new fragment or not.
Or second option if you are using firebase then push all notification as background notification and onMessageReceive method you can get top activity from activity stack and set that activity as entry point for the notification. But there is still problem as user may be click on notification after navigate from set entry point activity which again problem. So I prefer to go with first one approach.
make new activity
public class FinishImmediateActivity extends AppCompatActivity {
#Override protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish();
}
}
add to manifest.xml
<activity android:name=".FinishImmediateActivity"/>
check app is running
public static boolean isMainActivityRunning() {
ActivityManager activityManager = (ActivityManager) MyApp.getContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasksInfo = activityManager.getRunningTasks(Integer.MAX_VALUE);
for (int i = 0; i < tasksInfo.size(); i++) {
if (tasksInfo.get(i).baseActivity.getPackageName().equals(MyApp.getContext().getPackageName())) {
return true;
}
}
return false;
}
then call that activity in notification intent.
Intent resultIntent = new Intent(this, isMainActivityRunning() ? FinishImmediateActivity.class : HomeActivity.class);
By this way also we can achieve the above result:
try {
int icon;
icon = R.mipmap.ic_launcher;
int mNotificationId = 001;
Intent intent = new Intent(this, MainDrawerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
//FLAG_UPDATE_CURRENT is important
PendingIntent pendingIntent = PendingIntent.getActivity(this,
(int)System.currentTimeMillis(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(
this);
Notification notification =
mBuilder.setSmallIcon(icon).setTicker(json.getString("team")).setWhen(0)
.setAutoCancel(true)
.setContentTitle(json.getString("team"))
.setStyle(new
NotificationCompat.BigTextStyle().bigText(json.getString("message")))
.setContentIntent(pendingIntent)
.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.beep))
NotificationManager notificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(mNotificationId, notification);
} catch (Exception e) {
e.printStackTrace();
}
I am showing a notification from a library attached to my project, when clicked on the notification, the notification takes to an Activity (ReceivingActivity). Activity opens after clicking the notification ,but the extras attached to it are not received.
Notification triggering code - I call sendNotification when i receive a gcm message and Notification code is in the library
public static final int NOTIFICATION_ID = 1;
private NotificationManager mNotificationManager;
NotificationCompat.Builder builder;
private void sendNotification(Bundle extras) {
mNotificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Intent redirectIntent = new Intent(this, Class.forName("com.xyz.kljdf.ReceivingActivity"));
redirectIntent.putExtra("key", "value"); // These extras are not received in ReceivingActivity onCreate(), see the code below
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
redirectIntent, 0);
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(((Context)this).getResources().getIdentifier("icon", "drawable", getPackageName()))
.setContentTitle(extras.getString(PushConstants.TITLE))
.setStyle(new NotificationCompat.BigTextStyle()
.bigText(extras.getString(PushConstants.ALERT)))
.setContentText(extras.getString(PushConstants.ALERT));
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
Checking the extras in the ReceivingActivity...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_receiving);
Log.i("extras..... ######", getIntent().getExtras().toString()); //NPE here
}
I need help in figuring out how to pass the extras and receive them correctly.
Make sure the PendingIntent is actually passing the data when it's called. See this answer for an explanation: https://stackoverflow.com/a/3851414/1426565
For anyone else, if you're positive the intent already exists and is just brought to the front, the new Intent's data won't be passed to it by default. You can override onNewIntent(Intent) in order to get around that:
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent); // Set the new Intent's data to be the currently passed arguments
}
I want to display notification message in notification area on android. Now this display only application Title.
I am looking in logcat but I dont see no System.out ouput from line System.out.println(newMessage);
I have this class:
/**
* Author : Taufan E.
* App Name : The Restaurant App
* Release Date : October 2012
*/
package com.the.restaurant;
public class Home extends Activity {
String gcm_regId="";
static DBHelper dbhelper;
MainMenuAdapter mma;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
//GCMRegistrar.unregister(this);
// Make sure the device has the proper dependencies.
GCMRegistrar.checkDevice(this);
// Make sure the manifest was properly set - comment out this line
// while developing the app, then uncomment it when it's ready.
//GCMRegistrar.checkManifest(this);
gcm_regId = GCMRegistrar.getRegistrationId(this);
if(gcm_regId.equals("")){
GCMRegistrar.register(this, Utils.SENDER_ID);
}
System.out.println(gcm_regId);
registerReceiver(mHandleMessageReceiver, new IntentFilter("com.the.restaurant.DISPLAY_MESSAGE"));
if (!Utils.isNetworkAvailable(Home.this)) {
Toast.makeText(Home.this, getString(R.string.no_internet), Toast.LENGTH_SHORT).show();
}
mma = new MainMenuAdapter(this);
dbhelper = new DBHelper(this);
listMainMenu.setAdapter(mma);
}
#Override
protected void onPause(){
super.onPause();
unregisterReceiver(mHandleMessageReceiver);
}
#Override
protected void onResume(){
super.onResume();
registerReceiver(mHandleMessageReceiver, new IntentFilter("com.the.restaurant.DISPLAY_MESSAGE"));
}
private void displayNotification(final Context theContext,String message){
long when = System.currentTimeMillis();
int icon = R.drawable.cover_image;
NotificationManager notificationManager = (NotificationManager)
theContext.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);
String title = theContext.getString(R.string.app_name);
Intent notificationIntent = new Intent();
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent = PendingIntent.getActivity(theContext, 0, notificationIntent, 0);
notification.setLatestEventInfo(theContext, title, message, intent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
// Play default notification sound
notification.defaults |= Notification.DEFAULT_SOUND;
// Vibrate if vibrate is enabled
notification.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, notification);
}
private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
String newMessage = intent.getExtras().getString("rezervare");
// Waking up mobile if it is sleeping
WakeLocker.acquire(getApplicationContext());
/**
* Take appropriate action on this message
* depending upon your app requirement
* For now i am just displaying it on the screen
* */
// Showing received message
//lblMessage.append(newMessage + "\n");
System.out.println(newMessage);
Toast.makeText(getApplicationContext(), "New Message: " + newMessage, Toast.LENGTH_LONG).show();
displayNotification(context,newMessage);
//sendnotification("The Restaurant App","Mesaj");
//GCMRegistrar.unregister(Reservation.class);
// Releasing wake lock
WakeLocker.release();
}
};
}
You'll need to build the notification first:
// Pop up Notification
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("New Notification Title")
.setContentText("This is notification content.");
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ClassToLaunch.class);
// The stack builder object will contain an artificial back stack for the
// started Activity.
// This ensures that navigating backward from the Activity leads out of
// your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ClassToLaunch.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
);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify("tag", mBuilder.build());
As per your question you can't see output from following line:
System.out.println(newMessage);
And also you want to display the notification which I can see will be displayed after the following method is executed:
displayNotification(context,newMessage);
Answer:
The code that you are expecting to be executed is under the onReceive method of a BroadcastReceiver which means this method onReceive will be called when a special intent is generated which it is registered to in your onCreate method on the line:
registerReceiver(mHandleMessageReceiver, new IntentFilter("com.the.restaurant.DISPLAY_MESSAGE"));
You can broadcast Intent in the following manner:
Intent intent = new Intent();
intent.setAction("com.the.restaurant.DISPLAY_MESSAGE");
sendBroadcast(intent); ///sendBroadcast is a Context (Activity) method