Android Studio/ Firebase Data Notification send with default parameters - android

I'm currently making an app on android studio and I am getting stuck.
I've mananaged to have my notifications displayed when the app is open or closed, and when the app is in foreground, I receive the notification with the modifications I made on the MyFirebaseInstanceIdService file (see the file below), but i can't manage to display the notification when the app is closed or in background (meaning when it's a data notification) with the modifications I made (like the change of icon for the notification).
here is the code of the MyFirebaseInstanceIdService, the file managing my notifications
package com.shayru.myapplication.Service;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.shayru.myapplication.R;
import java.util.Map;
import java.util.Random;
public class MyFirebaseInstanceIdService extends FirebaseMessagingService {
//Ctrl o
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
if(remoteMessage.getData().isEmpty())
showNotification(remoteMessage.getNotification().getTitle(),remoteMessage.getNotification().getBody());
else
showNotification(remoteMessage.getData());
}
private void showNotification(Map<String,String> data) {
String title = data.get("title").toString();
String body = data.get("body").toString();
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID ="com.shayru.myapplication.test";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("CE Valeo");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
//R to take from "com.Shayru.myapp
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("Info");
notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
}
private void showNotification(String title, String body) {
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
String NOTIFICATION_CHANNEL_ID ="com.shayru.myapplication.test";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
{
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID,"Notification",
NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("CE Valeo");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setVibrationPattern(new long[]{0,1000,500,1000});
notificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL_ID);
notificationBuilder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
//R to take from "com.Shayru.myapp
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle(title)
.setContentText(body)
.setContentInfo("Info");
notificationManager.notify(new Random().nextInt(),notificationBuilder.build());
}
}

Related

exported app (android) doesnt have the whole functions

When I install the app directly from android studio onto my phone per usb everything works well. I am working with push notifications that work with this method. But when I export my app in android studio and get the apk file and then install it per this apk file onto my phone everything works but not the notification??
Do you understand what I mean?
I can play my app per android studio directly onto my phone and it works 100% well ( I also get the expected push notifications).
After the export I get an apk file. I install this apk file manually and I get my app which looks great at first, only the notifications dont come although they come when I install it per android studio directly.
What am I doing wrong? Isnt the apk file the right file? Maybe I need another file? The code must be good because everything is fine when pushing play in android studio.
EDIT: Here is my code for the notifications:
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import java.util.Random;
public class NotificationHelper extends ContextWrapper {
private static final String TAG = "NotificationHelper";
public NotificationHelper(Context base) {
super(base);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannels();
}
}
private String CHANNEL_NAME = "High priority channel";
private String CHANNEL_ID = "com.example.notifications" + CHANNEL_NAME;
#RequiresApi(api = Build.VERSION_CODES.O)
private void createChannels() {
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.enableVibration(true);
notificationChannel.setDescription("this is the description of the channel.");
notificationChannel.setLightColor(Color.RED);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(notificationChannel);
}
public void sendHighPriorityNotification(String title, String body, Class activityName) {
Intent intent = new Intent(this, activityName);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 267, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
// .setContentTitle(title)
// .setContentText(body)
.setSmallIcon(R.drawable.ic_launcher_background)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setStyle(new NotificationCompat.BigTextStyle().setSummaryText("summary").setBigContentTitle(title).bigText(body))
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build();
NotificationManagerCompat.from(this).notify(new Random().nextInt(), notification);
}
}
And here is my code where I call the notification:
package com.example.geofencing1;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.location.Geofence;
import com.google.android.gms.location.GeofencingEvent;
import com.google.android.gms.maps.model.LatLng;
import java.util.List;
public class GeofenceBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "GeofenceBroadcastReceiv";
#Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
// Toast.makeText(context, "Geofence triggered...", Toast.LENGTH_SHORT).show();
NotificationHelper notificationHelper = new NotificationHelper(context);
GeofencingEvent geofencingEvent = GeofencingEvent.fromIntent(intent);
if (geofencingEvent.hasError()){
Log.d(TAG, "onReceive: Error receiving geofence event...");
return;
}
List<Geofence> geofenceList = geofencingEvent.getTriggeringGeofences();
for (Geofence geofence: geofenceList) {
Log.d(TAG, "onReceive: " + geofence.getRequestId());
}
// Location location = geofencingEvent.getTriggeringLocation();
int transitionType = geofencingEvent.getGeofenceTransition();
switch (transitionType) {
case Geofence.GEOFENCE_TRANSITION_ENTER:
Toast.makeText(context, "GEOFENCE_TRANSITION_ENTER", Toast.LENGTH_SHORT).show();
notificationHelper.sendHighPriorityNotification("GEOFENCE_TRANSITION_ENTER", "", MapsActivity.class);
break;
case Geofence.GEOFENCE_TRANSITION_DWELL:
Toast.makeText(context, "GEOFENCE_TRANSITION_DWELL", Toast.LENGTH_SHORT).show();
notificationHelper.sendHighPriorityNotification("GEOFENCE_TRANSITION_DWELL", "", MapsActivity.class);
break;
case Geofence.GEOFENCE_TRANSITION_EXIT:
Toast.makeText(context, "GEOFENCE_TRANSITION_EXIT", Toast.LENGTH_SHORT).show();
notificationHelper.sendHighPriorityNotification("GEOFENCE_TRANSITION_EXIT", "", MapsActivity.class);
break;
}
}
}

Creating a notification with notification channel gives no effect

I've been browsing the net for an answer but i can't find it myself despite reading the developer guides on the official android documentation site and code snippets from all over the Internet.
I've been practicing creating notifications on a new clean project, and despite creating a notification channel and the notification itself I still can't get it to run and get a message saying: "Failed to post notification on null channel". But im creating the channel, and assigning it to my notification just like it says everywhere. Im completely out of ideas and honestly i've been browsing stack for at least few hours now and every answer looks the same but it really didn't help me at all, so maybe if someone who's better than a begginer looks at my code can tell me what's wrong with my code? It's annoying because notifications are an important part of a project im working on, and it stopped me in my tracks.
public class MainActivity extends AppCompatActivity {
NotificationCompat.Builder notification;
private static final int NOTIFICATION_ID = 1;
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
notification = new NotificationCompat.Builder(this);
notification.setAutoCancel(true);
findViewById(R.id.buckysButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
createNotification(view);
}
});
}
public void createNotification(View view){
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "My Notifications", NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.setDescription("Channel description");
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setVibrationPattern(new long[]{0, 1000, 500, 1000});
notificationChannel.enableVibration(true);
nm.createNotificationChannel(notificationChannel);
}
notification.setSmallIcon(R.drawable.ic_priority_high_black_24px);
notification.setTicker("Ticker");
notification.setWhen(System.currentTimeMillis());
notification.setContentTitle("Title");
notification.setContentText("Body Body Body Body Body Body Body Body Body ");
notification.setChannel(NOTIFICATION_CHANNEL_ID);
notification.build();
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setContentIntent(pendingIntent);
nm.notify(NOTIFICATION_ID, notification.build());
}}
By the way I'm developing for Android minimum level of 5.1 and I have been wondering if it's really necessary to put channels in my application, but I did it anyways since when im trying to do it without them I'm still getting exactly the same error.
EDIT: Im still struggling with this issue. Do I have to do something in the gradle files to be able to pass two arguments into the NotificationCompat.Builder constructor? It wont let me do it no matter what i do, i think thats the main problem here.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
Doesnt do it for me, it still doesnt want to take the channel_id as an argument.
new NotificationCompat.Builder(this);
is deprecated, try using
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID)
Here's a sample code, create a NotificationHelper class as follows,
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.provider.Settings;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
public class NotificationHelper extends ContextWrapper {
private NotificationManager mNotificationManager;
private final String MY_CHANNEL = "my_channel";
private final long[] vibrationScheme = new long[]{200, 400};
/**
* Registers notification channels, which can be used later by individual notifications.
*
* #param context The application context
*/
public NotificationHelper(Context context) {
super(context);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
// Create the channel object with the unique ID MY_CHANNEL
NotificationChannel myChannel =
new NotificationChannel(
MY_CHANNEL,
getResources().getString(R.string.notification_channel_title),
NotificationManager.IMPORTANCE_DEFAULT);
// Configure the channel's initial settings
myChannel.setLightColor(Color.GREEN);
myChannel.setVibrationPattern(vibrationScheme);
// Submit the notification channel object to the notification manager
getNotificationManager().createNotificationChannel(myChannel);
}
}
/**
* Build you notification with desired configurations
*
*/
public NotificationCompat.Builder getNotificationBuilder(String title, String body, Intent pendingIntent) {
Bitmap notificationLargeIconBitmap = BitmapFactory.decodeResource(
getApplicationContext().getResources(),
R.mipmap.ic_launcher);
return new NotificationCompat.Builder(getApplicationContext(), MY_CHANNEL)
.setSmallIcon(R.drawable.ic_notification_icon)
.setLargeIcon(notificationLargeIconBitmap)
.setContentTitle(title)
.setContentText(body)
.setSound(Settings.System.DEFAULT_NOTIFICATION_URI)
.setVibrate(vibrationScheme)
.setStyle(new NotificationCompat.BigTextStyle().bigText(body))
.setColor(ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary))
.setContentIntent(pendingIntent)
.setAutoCancel(true);
}
public NotificationManager getNotificationManager() {
if (mNotificationManager == null) {
mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
return mNotificationManager;
}
}
and then use it as follows,
NotificationHelper notificationHelper = new NotificationHelper(context);
NotificationCompat.Builder builder = notificationHelper.getNotificationBuilder(title, message);
notificationHelper.getNotificationManager().notify(notificationID, builder.build());
Ok, maybe I'm bit late, but...
Try to shorten your channelId string.
Now it
private static final String NOTIFICATION_CHANNEL_ID = "my_notification_channel";
Try
private static final String NOTIFICATION_CHANNEL_ID = "my_channel";
For me it was a very strange bug. When channelId was too long it don't show notification on my device.
Edit:
It was not too long or too short, just some string got banned in system. Also it not showed in Android notification settings screen.
I've found out what was missing. I was using android studio 2.3.3 instead of the newest 3.0.1 so I couldn't import some of the new functions through Gradle.
After that i only had to change my dependencies classpath to 'com.android.tools.build:gradle:3.0.1' and the problem was gone.
Thanks for help.

Android Firebase Notification message received but notification prompt is not showing?

I have followed this tutorial and when I try to send a message from Firebase console, onMessageReceived were called and createNotification were performed, no notification prompt shows up.
It suppose to prompt this but it didn't
Below is my MyAndroidFirebaseMsgService code
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.HashMap;
public class MyAndroidFirebaseMsgService extends FirebaseMessagingService
{
private static final String TAG = "MyAndroidFCMService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Log data to Log Cat
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
//create notification
createNotification(remoteMessage.getNotification().getBody());
}
private void createNotification( String messageBody)
{
Intent intent = new Intent( this , ResultActivity.class );
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultIntent = PendingIntent.getActivity( this , 0, intent, PendingIntent.FLAG_ONE_SHOT);
Uri notificationSoundURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder( this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Test Notification")
.setContentText(messageBody)
.setAutoCancel( false )
.setSound(notificationSoundURI)
.setContentIntent(resultIntent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, mNotificationBuilder.build());
}
}
Finally found that the notification were actually only appeared on the android drop down menu instead of popping up due to my device is running Android 4. I test again with Android 5 device and the notification pop up just like the tutorial.
Maybe you were missing notification notify.
Please try bellow code. it's working properly
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(RemoteMessage remoteMessage) {
Intent resultIntent = new Intent(this, MenuBarActivity.class);
resultIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
HashMap<String, String> dataMap = new HashMap<String, String>(remoteMessage.getData());
resultIntent.putExtra(Constant.KEY_MESSAGE_BODY, dataMap);
resultIntent.putExtra(Constant.KEY_IS_NOTIFICATION, true);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, resultIntent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setStyle(new NotificationCompat.BigTextStyle().bigText(remoteMessage.getData().get(Constant.KEY_BODY)))
.setContentText(remoteMessage.getData().get(Constant.KEY_BODY))
.setAutoCancel(true)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setSound(defaultSoundUri);
// if (!BaseActivity.isInForeground()) {
notificationBuilder.setContentIntent(pendingIntent);
// }
// Generate ID
long time = new Date().getTime();
String tmpStr = String.valueOf(time);
String last4Str = tmpStr.substring(tmpStr.length() - 5);
int notificationId = Integer.valueOf(last4Str);
// Sets an ID for the notification
// int notificationId = Constant.NOTIFICATION_ID;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(notificationId, notificationBuilder.build());
}

Notification sound is not created when phone is locked

I'm developing a chat application and I am using Firebase Cloud Messaging. Notification works properly. When the phone is locked, the notification is received properly, but the sound is not generated.
Any help is appreciated.
FCMMessagingservice.java
package com.synergywebdesigners.nima;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;
import android.view.View;
import android.view.WindowManager;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import static android.app.Notification.VISIBILITY_PUBLIC;
import static com.android.volley.VolleyLog.TAG;
/**
* Created by Ashish Shahi on 13-04-2017.
*/
public class FcmMessagingService extends FirebaseMessagingService {
long[] pattern = {500,500,500,500,500,500,500,500,500};
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
PowerManager pm = (PowerManager)getSystemService(
Context.POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(
PowerManager.SCREEN_DIM_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE,
TAG);
String title = remoteMessage.getNotification().getTitle();
String message = remoteMessage.getNotification().getBody();
Intent intent = new Intent(this, Memberlist.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_action_title)
.setContentTitle(title)
.setContentText(message)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent)
.setDefaults(0)
.setLights(Color.RED, 500, 500)
.setPriority(Notification.PRIORITY_HIGH)
.setOngoing(true)
;
wl.acquire();
// ... do work...
wl.release();
/*notificationBuilder.setLights(Color.BLUE, 500, 500);
long[] pattern = {500,500,500,500,500,500,500,500,500};
notificationBuilder.setVibrate(pattern);*/
notificationBuilder.setStyle(new NotificationCompat.InboxStyle());
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
Go to FCM CONSOLE and enable sound and define key 'sound'==>'default';
and go to php cansole and and type Following code 'sound'==>'default'; thats work properly

Why does Eclipse not recognize the build() method from the Notification.Builder class to return a Notification object?

This is my code:
NotificationManager mNotificationManager = (NotificationManager) c.getSystemService(ns);
//Instantiate the notification
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification.Builder builder = new Notification.Builder(c)
.setTicker(tickerText)
.setWhen(when)
.setContentTitle("Test Notification")
.setContentText(arg1.getStringExtra("info"))
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true);
Notification notification = builder.getNotification();
mNotificationManager.notify(88, notification);
It works find, but using Notification notification = builder.getNotification(); is deprecated. as I should be doing Notification notification = builder.build();.
Problem is Eclipse isn't recognizing it as such, meaning it won't let me compile. The doc is clear that build() exists and its the preferred method, but its not working on my end. I would like to use non-deprecated code, so any help will be much appreciated.
imports
import android.app.Notification;
import android.app.Notification.Builder;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
Be aware that import android.app.Notification.Builder; is saying its not being used.
If you want develop for version SDK lower than 11, you can use this code instead android.app.Notification.Builder class for creation notification:
private void createNotification(){
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(R.drawable.ic_launcher, "Hello", System.currentTimeMillis());
Intent notificationIntent = new Intent(this, YourActivity.class);
Random r = new Random();
int notificationId = r.nextInt();
notificationIntent.putExtra("n_id", notificationId);
PendingIntent contentIntent = PendingIntent.getActivity(this, notificationId, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notification.defaults |= Notification.DEFAULT_SOUND;
notification.setLatestEventInfo(this, "Party", "Welcome!", contentIntent);
mNotificationManager.notify(notificationId, notification);
}
You can cancel this notification in YourActivity like this:
public class YourActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
int notificationId = getIntent().getIntExtra("n_id", -1);
if (notificationId!=-1) {
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancel(notificationId);
}
}
}
This only happens when, You are compiling your app code against the version which does not hold required method/api (in your case it is build()).
If build is not available in the version you are compiling with, I will suggest you to compile with higher version of Android, You always have minimum sdk version in manifest for backward compatibility.
Thanks

Categories

Resources