-1

Here's my issue..

I have an AlarmManager with a Timer, which fires a Receiver when the timer expires..

When the Timer expires, and the onReceive is fired within the Receiver, I want to create a NOTIFICATION in the Notification Bar.. However, for some reason I'm having issues..

For example, the word this is always underlined in red..
I was able to replace SOME of them with context to get rid of some errors,
however there are a couple places where I am still having issues..

I'm definitely doing a few things incorrectly, and could really use some help!

The following is my Code, within the Receiver's onReceive method..
Any advice would be greatly appreciated! Thank you!

(PS.. When clicked, I want it to open the Location/GPS Settings screen)..

    // Sets an ID for the notification
    int mNotificationId = 001;


    NotificationManager mNotifyMgr = 
            (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);


    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_notification_messageboxes)
                    .setContentTitle("TITLE OF NOTIFICATION")
                    .setContentText("Description text of the notification");


    Intent resultIntent = 
            new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);


    PendingIntent resultPendingIntent = 
            PendingIntent.getActivity
                    (context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);


    mBuilder.setContentIntent(resultPendingIntent);


    mNotifyMgr.notify(mNotificationId, mBuilder.build());
StudioB
  • 117
  • 9
  • **SITE MOD:** I am genuinely unsure as to why my question got a downvote.. I provided my code block to show my work, and asked a legitimate question.. I am _fairly_ new to android development still, and am doing my best to learn.. However, I oftentimes feel judged harshly on this site simply because I may not understand something immediately, or ask followup question(s).. Its a shame, for someone like me who is genuinely trying to learn, only to be met with adversity and dismissal, rather than additional help from those who downvote the question instead of providing productive assistance.. – StudioB Oct 08 '16 at 07:11

1 Answers1

2

Use this code

private void sendNotification(String message) {
    Intent intent = new Intent(this, "YOUR CLASS WHERE LAND".class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    int requestCode = 0;
    PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("TITLE")
            .setContentText(message)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setSound(sound);

    NotificationManager notificationManager = (NotificationManager)
            getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, noBuilder.build());
}

NOTIFICATION_ID is unique id every-time. Call this method when you receive something in Receiver's onReceive() Method.

Updated code: Here is full code :

public class GCMPushReceiverService extends GcmListenerService {

Random random = new Random();
int NOTIFICATION_ID = random.nextInt(9999 - 1000) + 1234;

@Override
public void onMessageReceived(String from, Bundle data) {
    String message = data.getString("message");
    Log.e("Notification ", "Received onMessage : " + data);
    sendNotification(message);
}

private void sendNotification(String message) {
    Intent intent = new Intent(this, "YOUR CLASS WHERE LAND".class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    int requestCode = 0;
    PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
    Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_launcher)
            .setContentTitle("Title")
            .setContentText(message)
            .setAutoCancel(true)
            .setContentIntent(pendingIntent)
            .setSound(sound);

    NotificationManager notificationManager = (NotificationManager)
            getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, noBuilder.build());
}

}

Hope this will work for you.

Sanwal Singh
  • 1,765
  • 3
  • 17
  • 35
  • Thanks for the Code.. I tried it, and I've got a few issues.. (1) the call to 'getSystemService' is red letters.. also (2) NOTIFICATION_ID is red letters as well (unless I declare an int for it).. and (3) When I call sendNotification(String message); What exactly is 'message' representing? and lastly, (4) in the Intent, I want to bring up the Location/GPS Settings screen, however I just get a big red underline when I enter android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS for the landing activity.. Can you help me with these 4 issues? – StudioB Oct 07 '16 at 06:12
  • Check updated code. 1. Point 3 - "message" - That your want to display with notification text – Sanwal Singh Oct 07 '16 at 06:21
  • I fixed the NOTIFICATION_ID by using the Random part that you suggested. thanks -- But Im still getting red letters on `getSystemService`.. Any suggestions? – StudioB Oct 08 '16 at 00:27
  • I managed to fix the `getSystemService` red letters, and was able to replace `this` with `context` as the first parameter of `PendingIntent.getActivity`.. however **ONE** issue still remains - `this` is still red when passed in `NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(**this**)`.. and it cannot be replaced with `context`... **Any advice to fix this?** I am doing my best to learn. – StudioB Oct 08 '16 at 07:05