PeakGen PeakGen - 5 months ago 13
Android Question

Android: Notifications are not showing in Notification bar

I am trying to display a notification in Notification bar when an alarm is triggered. Below is my code.

AlarmReciever.java

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.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;

/**
* Created by Yohan on 6/29/2016.
*/
public class AlarmReciever extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub


// here you can start an activity or service depending on your need
// for ex you can start an activity to vibrate phone or to ring the phone

String message="Hi I will be there later, See You soon";// message to send



// Show the toast like in above screen shot
Log.d("Alarm",message);
Toast.makeText(context, "Alarm Triggered and SMS Sent", Toast.LENGTH_LONG).show();

Intent intent2 = new Intent(context, NotificationReceiverActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(context, (int) System.currentTimeMillis(), intent2, 0);

Notification noti = new Notification.Builder(context)
.setContentTitle("New mail from " + "test@gmail.com")
.setContentIntent(pIntent).getNotification();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;

notificationManager.notify(0, noti);
}

}


Here I have used
getNotification()
instead of
build()
is because my minimum API is 15.

NotificationReceiverActivity.java

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

public class NotificationReceiverActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notification_receiver);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

}


Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="voice1.xxx.com.alarmcheck2" >

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<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=".BootCompletedReceiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<receiver
android:name=".AlarmReciever"
android:process=":remote" />

<activity
android:name=".NotificationReceiverActivity"
android:label="@string/title_activity_notification_receiver"
android:theme="@style/AppTheme.NoActionBar" >
</activity>
</application>

</manifest>


In my
AlarmReciever.java
the
Toast
is getting fired so I know it is working. However the notification is not. Any idea why it is?

Answer

As documentation said, you must put a small icon :

    Required notification contents
A Notification object must contain the following:

A small icon, set by setSmallIcon()
A title, set by setContentTitle()
Detail text, set by setContentText()

See here for more details : https://developer.android.com/guide/topics/ui/notifiers/notifications.html

Comments