Android notificiations not being displayed - android

I have some code set up for the Status Notification, nothing is wrong with the code according to eclipse. However the code wont run when I start it up. Well It bypasses the notification and goes straight to the main activity. Why is my code skipping the notifications?
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class NoteMe extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int notifID = getIntent().getExtras().getInt("NotifID");
Intent i = new Intent("com.example.something.MainActivity");
PendingIntent detailsIntent = PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(R.drawable.ic_launcher,
"STOP LOOK LISTEN!", System.currentTimeMillis());
CharSequence message = "This is your alert, courtesy of the AlarmManager";
notif.setLatestEventInfo(this, message, message, detailsIntent);
notif.vibrate = new long[] { 100, 250, 100, 500 };
nm.notify(notifID, notif);
finish();
}
}
Followed by Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.something"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.CAMERA" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NoteMe" >
<intent-filter>
<action android:name="com.example.something.NoteMe" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And logcat!
--Actually there is nothing in logcat (Strange)

In the manifest, if you remove the <action android:name="android.intent.action.MAIN" /> from the intent filter of your MainActivity and put it in the intent filter of your NoteMe activity, that activity should run on launch instead of the other. You just need to launch MainActivity from your NoteMe activity, right before you call finish().

Your onCreate() method is missing setContentView(). This should be called after super.onCreate(savedInstanceState). This tells Android which XML layout you would like to associate with the Activity.

Related

when push notification clicked, specific activity doesn't open

Not opening specific activity on notification click when the app is in background/not running
The notification-click starts specified activity only when the app is opened up and the notification-click is performed. If the app is in background/not running and the notification-click is performed, the application's MainActivity opens up. In short, it is like the app opens normally following the activity stack instead of opening the specified activity in the PendingIntent.
I saw a solution similar to this problem in this forum did not help unfortunately
MyApplication
import android.app.Application;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.onesignal.OSNotificationOpenResult;
import com.onesignal.OneSignal;
import org.json.JSONException;
import org.json.JSONObject;
public class MyApplication extends Application {
#Override
public void onCreate() {
super.onCreate();
OneSignal.startInit(this).setNotificationOpenedHandler(new ExampleOpenHandler())
.autoPromptLocation(true).init();
}
private void startNotification() { Log.i("NextActivity", "startNotification");
// Sets an ID for the notification int mNotificationId = 001;
// Build Notification , setOngoing keeps the notification always in status bar
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.bugununfaaliyet)
.setContentTitle("Stop LDB")
.setContentText("Click to stop LDB")
.setOngoing(true);
// Create pending intent, mention the Activity which needs to be
//triggered when user clicks on notification(StopScript.class in this case)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, SecondActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(1, mBuilder.build());
}
private class ExampleOpenHandler implements OneSignal.NotificationOpenedHandler {
#Override
public void notificationOpened(OSNotificationOpenResult result) {
JSONObject data=result.notification.payload.additionalData;
Intent intent = new Intent(getApplicationContext(),SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
try{
intent.putExtra("datam",data.getString("1"));
}catch (JSONException e) {
e.printStackTrace();
}
startActivity(intent);
}
}
}
SecondActivity
package blabla.com.blabla;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class SecondActivity extends AppCompatActivity {
TextView dataview;
String getdata;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
getdata = getIntent().getExtras().getString("datam");
dataview = (TextView) findViewById(R.id.dataview);
dataview.setText(getdata);
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="blabla.com.blabla">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".Activity.girisKullaniciAdi"
android:screenOrientation="portrait"
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=".Activity.TabbedActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<activity
android:name=".Activity.KameraActivity"
android:parentActivityName=".Activity.AnaActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activity.MainActivity"
android:screenOrientation="portrait" />
<activity
android:name=".Activity.AnaActivity"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.Anasayfa" />
<activity
android:name=".Activity.FizikselSayimKameraActivity"
android:parentActivityName=".Activity.AnaActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<activity
android:name=".Activity.EkipmanGecmisiKameraActivity"
android:parentActivityName=".Activity.AnaActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<activity
android:name=".Activity.EkipmanGecmisiActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<activity
android:name=".Activity.YeniIsEmriKameraActivity"
android:parentActivityName=".Activity.AnaActivity"
android:screenOrientation="portrait"
android:theme="#style/ThemeToolbar" />
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
<meta-data android:name="com.onesignal.NotificationOpened.DEFAULT"
android:value="DISABLE" />
</provider>
<activity android:name=".SecondActivity"></activity>
</application>
</manifest>
Please check out this answer..
private void startNotification() {
Log.i("NextActivity", "startNotification");
// Sets an ID for the notification
int mNotificationId = 001;
// Build Notification , setOngoing keeps the notification always in status bar
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ldb)
.setContentTitle("Stop LDB")
.setContentText("Click to stop LDB")
.setOngoing(true);
// Create pending intent, mention the Activity which needs to be
//triggered when user clicks on notification(StopScript.class in this case)
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, StopScript.class), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(contentIntent);
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
}
// StopScript is your Activity Name

BroadcastReceiver does not work, no notification

I have tried to do a simple app (or, more specifically a Receiver):
When the user presses the "Camera button" or "Dealer button" - the app raises a notification.
There isn't an ERROR, but the app doesn't work.
I have tried to add permission but it still doesn't work.
MyDownloadBroadcastReceiver.java:
package elyahsiv.raisenotificationwhendownloadfile;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.NotificationCompat;
import android.util.Log;
public class MyDownloadBroadcastReceiver extends BroadcastReceiver{
#Override
public void onReceive (Context context, Intent intent) {
Log.d("NOTES:", intent.getAction());
if (intent.getAction() == "android.intent.action.PACKAGE_ADDED")
showNotification(context);
else if (intent.getAction() == "android.intent.action.CAMERA_BUTTON")
showNotification(context);
}
private void showNotification(Context context) {
PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
new Intent(context, MainActivity.class), 0);
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(android.R.drawable.stat_notify_error)
.setContentTitle("My notification")
.setContentText("Hello World!");
mBuilder.setContentIntent(contentIntent);
mBuilder.setDefaults(Notification.DEFAULT_SOUND);
mBuilder.setAutoCancel(true);
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"
xmlns:tools="http://schemas.android.com/tools"
package="elyahsiv.raisenotificationwhendownloadfile">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyDownloadBroadcastReceiver" android:enabled="true" android:exported="false">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.telephony" android:required="false" />
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"></action>
<action android:name="android.intent.action.CAMERA_BUTTON"></action>
<action android:name="android.intent.action.CALL_BUTTON"></action>
</intent-filter>
</receiver>
</application>
</manifest>
Permissions have to be declared just before application tag.
You got to have android.permission.CAMERA and maybe more.
In intent filter you got to have
<data android:scheme="package" />
Consider splitting receiver in three different ones.
Also
Due to a broadcast behavior change since Android 3.1, your app must be started before it can receive the app installation/removal intents. See kabuko's answer in this thread.
Check this answer.
Try to register your receiver when activity or service is created.
Also you should consider the following:
Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.
For example:
MyDownloadBroadcastReceiver receiver = new MyDownloadBroadcastReceiver();
IntentFilter receiverFilterCameraButton = new IntentFilter("android.intent.action.CAMERA_BUTTON");
registerReceiver( receiver, receiverFilterCameraButton );

Android FCM Push Notification not received when app is closed

I implemented FCM Push Notifications. The main problem is when app is closed, notifications not arrive when this happen on device.
I tried a lot of thigs for that works; when notification sent the server response with "success", but I never received.
This is full code. Android, Manifiest and server.
Android:
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import com.accionait.canje365.R;
import com.accionait.canje365.activities.ChatActivity;
import com.accionait.canje365.activities.HomeActivity;
import com.accionait.canje365.constants.Constants;
import com.google.firebase.messaging.RemoteMessage;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
public class FirebaseMessagingService extends com.google.firebase.messaging.FirebaseMessagingService {
private static final String TAG = "FCM Service";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
super.onMessageReceived(remoteMessage);
showNotification(remoteMessage);
}
private void showNotification(RemoteMessage remoteMessage) {
Uri uriDefaultSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_notification)
.setLargeIcon((((BitmapDrawable)getResources().getDrawable(R.mipmap.ic_notification)).getBitmap()))
.setContentTitle("TITLE")
.setContentText("MESSAGE")
.setContentInfo("0")
.setAutoCancel(true)
.setTicker("Canje365")
.setSound(uriDefaultSound);
Intent home = new Intent(this, HomeActivity.class);
home.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(this, 0, home, PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(intent);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, builder.build());
}
}
Server:
var message = {
to: token,
data: {valor: 'test'}
};
fcm.send(message, function(err, response){
if(err) {
console.log('Error: ' + err);
}
else {
console.log('Notificacion enviada: ', response);
}
});
Manifiest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.accionait.canje365">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<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">
<service android:name=".sync.RunIntentService" />
<service android:name=".sync.FirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT" />
</intent-filter>
</service>
<service android:name=".sync.FirebaseIdService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="MY_API_KEY" />
<activity
android:name=".activities.MainActivity"
android:theme="#style/SplashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".activities.HomeActivity"
android:label="#string/title_activity_login"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
What to do with this?
In my case I have a Huawei P8 Lite and to allow to be run on the background.
This is the link where You can to find detail abount this case.
Solution on Huawei devices
I faced the same problem. Its a problem in many phone models. I'm just writing this so that out there if anyone faces the same problem can waste a little less time searching for this problem because I wasted a lot!
Android M and above devices have battery optimization enabled by default for all apps, which usually stops the background services of our apps. We just have to ask user to disable them, if user choose not to disable them, then I'm sorry..i don't think user is really happy with your app functionality. In order to ask user to enable that--->
Intent intent = new Intent();
String packageName = getPackageName();
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (pm != null && pm.isIgnoringBatteryOptimizations(packageName))
intent.setAction(Settings.ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS);
else {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
}
startActivity(intent);

Android - alarm notification issue

I'm trying to configure an alarm notification in Android and unfortunately no alarm is generated.
Here is the creation of the alarm:
myDate = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(time_Date_str);
t.setDueDate(myDate);
t.setHasDate(true);
Intent alarmNotificationIntent = new Intent(this, ReminderNotification.class);
alarmNotificationIntent.putExtra("task", t);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this, (int) task_id, alarmNotificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(t.getDueDate().getTime());
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
In addition I've created:
NotificationReceiver
import android.app.Activity;
import android.os.Bundle;
public class NotificationReceiver extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
ReminderNotification
import android.content.Context;
import android.content.Intent;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
public class ReminderNotification extends BroadcastReceiver
{
public ReminderNotification()
{
// TODO Auto-generated constructor stub
}
#Override
public void onReceive(Context context, Intent intent) {
// The PendingIntent to launch our activity if the user selects this notification
Task task = (Task)intent.getSerializableExtra("task");
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
Intent snzInt = new Intent(context, SnoozeReminderReceiver.class);
snzInt.putExtra("task", task);
PendingIntent snoozeIntent = PendingIntent.getBroadcast(context, 0,snzInt, PendingIntent.FLAG_CANCEL_CURRENT);
Intent doneInt = new Intent(context,DoneActionReceiver.class);
doneInt.putExtra("task", task);
PendingIntent doneIntent = PendingIntent.getBroadcast(context, 0,doneInt, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
}
}
The manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="il.ac.she.dd.todoli"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<receiver android:name="ReminderNotification"/>
<receiver android:name="SnoozeReminderReceiver"/>
<receiver android:name="DoneActionReceiver"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".ListNodeActivity"
android:label="#string/title_activity_list_node"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="il.ac.shenkar.david.todolistex2.MainActivity" />
</activity>
<activity
android:name=".Signup_Activity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"></activity>
<activity
android:name=".create_team"
android:label="Create new task team"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".invite_member"
android:label="Invite Members Your Team"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Login_activity"
android:label="Login to Wiggle"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".SplashScreen"
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=".EditTaskActivity"
android:label="Edit Task"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
I have a feeling that I'm missing something very small but cannot find the issue.
Please let me know what I'm doing wrong.
Thanks in advance.
you have to first build notification and then notify it...
NotificationManager _manager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentTitle("New")
.setContentText("hello")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pintent)
.build();
notification.vibrate=new long[]{100,250,100,500};
_manager.notify(notification_id, notification);
You can find a complete example of Alarm and Notification in my this GitHub Repo

error with R.drawable.icon ; icon cannot be resolved or is not a field

There comes error with the auto-generated code section: R.drawable. Please suggest solution for the error with:
R.drawable.icon :- icon cannot be resolved or is not a field
The .java file possessing this error looks like this:
package net.learn2develop.UsingNotifications;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
public class DisplayNotifications extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//---get the notification ID for the notification;
// passed in by the MainActivity---
int notifID = getIntent().getExtras().getInt("NotifID");
//---PendingIntent to launch activity if the user selects
// the notification---
Intent i = new Intent("net.learn2develop.AlarmDetails");
i.putExtra("NotifID", notifID);
PendingIntent detailsIntent =
PendingIntent.getActivity(this, 0, i, 0);
NotificationManager nm = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification notif = new Notification(
R.drawable.icon,
"Time's up!",
System.currentTimeMillis());
CharSequence from = "AlarmManager - Time's up!";
CharSequence message = "This is your alert, courtesy of the AlarmManager";
notif.setLatestEventInfo(this, from, message, detailsIntent);
//---100ms delay, vibrate for 250ms, pause for 100 ms and
// then vibrate for 500ms---
notif.vibrate = new long[] { 100, 250, 100, 500};
nm.notify(notifID, notif);
//---destroy the activity---
finish();
}
}
The manifest file also indicates it:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.learn2develop.UsingNotifications"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<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=".AlarmDetails"
android:label="Details of the alarm">
<intent-filter>
<action android:name="net.learn2develop.AlarmDetails" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".DisplayNotification" >
<intent-filter>
<action android:name="net.learn2develop.DisplayNotification" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
I just thought I would add a quick additional answer to this topic. I am very new to Android development and had found that one of my classes was not compiling as it could not find any of my drawable attributes. In the end I tracked the problem down to the fact that the class was importing android.R (automatically added to the imports list by Eclipse). As soon as that line was taken out, the class compiled.
You should check if under the dir gen there's a file called R.java.
If so open it and checks if there's an attribute called icon.
It could be you moved your project or copied something from other projects.
In any cases you can remove manually the file under gen and let Eclipse recreate them.
If not you can go under Projects and then Clean chosing your project.
It should work.

Categories

Resources