Issue
I tried to create a notification on android to launch a foreground service. it didn't show the notification after the 'startForground' method, neither on the phone I am using nor on an emulator. so I tried to do it using a NotificationManager, it also didn't work on both of them. this is the code:
Notification.Builder notify = new Notification.Builder(this).setContentText("hello!")
.setStyle(new Notification.BigTextStyle().bigText("sup!"))
.setContentTitle("sup").addAction(R.drawable.ic_launcher_foreground,"hello", PendingIntent.getActivity(this, 0, new Intent(this, MainActivity.class), 0))
.setPriority(Notification.PRIORITY_HIGH);
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
notify.setSmallIcon(R.mipmap.ic_launcher);
}else{
notify.setSmallIcon(R.drawable.ic_launcher_foreground);
}
NotificationManager manager =
(NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = "132";
String description = "132";
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("132", name, importance);
channel.setDescription(description);
manager.createNotificationChannel(channel);
}
manager.notify(132, notify.build());
Toast.makeText(this, "finished", Toast.LENGTH_LONG);
setLatestAction("finished");
so, the answer was that on the version of android I was developing for, which was Oreo, you cannot show notification by normal means.
Solution
If you run your app into api 26 above like android 0 above device then need channel id.. try this code
check your app level gradle file..
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.example.adruser.newnotificaion"
minSdkVersion 20
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
testCompile 'junit:junit:4.12'
}
after that project level gradle file ..
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
after that make one common notification untils class like below code..
public class NotificationUtils extends ContextWrapper {
private NotificationManager mManager;
public static String mChannel_Id = null;
public static String mChannel_Id_EX = null;
public static String group = null;
private CharSequence mChannleName = null;
private CharSequence mGroupName;
private String mDescription = "this first channel id program";
private String mDescription2 = "this second channel id";
private static final String KEY_TEXT_REPLY = "key_text_reply";
@RequiresApi(api = Build.VERSION_CODES.O)
public NotificationUtils(Context base) {
super(base);
}
/**
* this method create a channel.
* this method call into a NotificationUtils class Constructor.
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public void createChannels(String channel1) {
// create android channel
mChannleName = channel1;// show channel mChannleName.
NotificationChannel androidChannel = new NotificationChannel(channel1,
mChannleName, NotificationManager.IMPORTANCE_DEFAULT);
//mChannel_Id=channel1;
// Sets whether notifications posted to this channel should display notification lights
androidChannel.enableLights(true);
androidChannel.setDescription(mDescription);
androidChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
// Sets whether notification posted to this channel should vibrate.
androidChannel.enableVibration(true);
// Sets the notification light color for notifications posted to this channel
androidChannel.setLightColor(Color.GREEN);
// Sets whether notifications posted to this channel appear on the lockscreen or not
androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
androidChannel.setShowBadge(true);
getManager().createNotificationChannel(androidChannel);
Toast.makeText(getApplicationContext(), "Channel created", Toast.LENGTH_SHORT).show();
}
/**
* this method create a channel with group _id.
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public void createChannelsWithGroup(String channel1) {
// create android channel
mChannleName = channel1;//show channel mChannleName.
NotificationChannel androidChannel = new NotificationChannel(channel1,
mChannleName, NotificationManager.IMPORTANCE_DEFAULT);
//mChannel_Id=channel1;
// Sets whether notifications posted to this channel should display notification lights
androidChannel.enableLights(true);
androidChannel.setDescription(mDescription);
androidChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
// Sets whether notification posted to this channel should vibrate.
androidChannel.enableVibration(true);
// Sets the notification light color for notifications posted to this channel
androidChannel.setLightColor(Color.GREEN);
// Sets whether notifications posted to this channel appear on the lockscreen or not
androidChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
androidChannel.setShowBadge(true);
androidChannel.setGroup(group);// used when create channel with group_id.
getManager().createNotificationChannel(androidChannel);
Toast.makeText(getApplicationContext(), "Channel created", Toast.LENGTH_SHORT).show();
//getManager().createNotificationChannelGroup(new NotificationChannelGroup(group,mDescription));
}
/**
* this method create a group.
*
* @param Group
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public void createGroup(String Group) {
mGroupName = Group;
getManager().createNotificationChannelGroup(new NotificationChannelGroup(Group, mGroupName));
Toast.makeText(getApplicationContext(), "Group Created", Toast.LENGTH_SHORT).show();
}
/**
* this method define notification manager class service.
*
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mManager.createNotificationChannelGroup(new NotificationChannelGroup(group,mChannleName));
}
return mManager;
}
@RequiresApi(api = Build.VERSION_CODES.O)
public Notification.Builder getAndroidChannelNotification(String title, String body) {
return new Notification.Builder(getApplicationContext(), mChannel_Id)
.setContentTitle(title)
.setContentText(body)
.setChannelId(mChannel_Id)
.setSmallIcon(android.R.drawable.stat_notify_more)
.setAutoCancel(true);
}
/**
* this method create a simple notification useing channel id.
*
* @param message
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public Notification.Builder createNotification(String message) {
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.putExtra("message", message);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
return new Notification.Builder(getApplicationContext(), mChannel_Id)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(mChannel_Id)
.setContentText(message)
//.setGroup(group)
.setChannelId(mChannel_Id)
.setContentIntent(resultPendingIntent);
}
/**
* this method used create a Expand_Notification.
*
* @param message
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.O)
public Notification.Builder longNotficationMessage(String message) {
Notification.InboxStyle inboxStyle =
new Notification.InboxStyle();
String[] events = new String[6];
inboxStyle.setBigContentTitle("Event tracker details:");
for (int i = 0; i < events.length; i++) {
events[i] = "Hii how";
inboxStyle.addLine(events[i]);
}
Intent resultIntent = new Intent(this, ResultActivity.class);
resultIntent.putExtra("message", message);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
stackBuilder.getPendingIntent(
0,
PendingIntent.FLAG_UPDATE_CURRENT
);
return new Notification.Builder(getApplicationContext(), mChannel_Id_EX)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(mChannel_Id_EX)
.setContentText(message)
//.setGroup(group)
.setChannelId(mChannel_Id_EX)
.setStyle(inboxStyle)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent);
}
/**
* this method used to show group of notification.
*/
public void showStackNotifications() {
Bitmap bitmapMila = BitmapFactory.decodeResource(getResources(), R.drawable.placeholder_border);
// Nuke all previous notifications and generate unique ids
NotificationManagerCompat.from(this).cancelAll();
int notificationId = 0;
// Group notification that will be visible on the phone
Notification summaryNotification = new NotificationCompat.Builder(this)
.setContentTitle("2 Pet Notifications")
.setContentText("nilay and Dylan both sent messages")
.setSmallIcon(R.mipmap.ic_launcher)
.setLargeIcon(bitmapMila)
.setGroup(group)
.setGroupSummary(true)
.build();
// Separate notifications that will be visible on the watch
Intent viewIntent1 = new Intent(this, MainActivity.class);
PendingIntent viewPendingIntent1 =
PendingIntent.getActivity(this, notificationId + 1, viewIntent1, 0);
Notification notification1 = new NotificationCompat.Builder(this)
.addAction(R.mipmap.ic_launcher, "Treat Fed", viewPendingIntent1)
.setContentTitle("Message from nilay")
.setContentText("What's for dinner? "
+ "Can we have steak?")
.setSmallIcon(R.mipmap.ic_launcher)
.setGroup(group)
.build();
Intent viewIntent2 = new Intent(this, MainActivity.class);
PendingIntent viewPendingIntent2 =
PendingIntent.getActivity(this, notificationId + 2, viewIntent2, 0);
Notification notification2 = new NotificationCompat.Builder(this)
.addAction(R.mipmap.ic_launcher_round, "Water Filled", viewPendingIntent2)
.setContentTitle("Message from Dylan")
.setContentText("Can you refill our water bowl?")
.setSmallIcon(R.drawable.placeholder_border)
.setGroup(group)
.build();
// Issue the group notification
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId + 0, summaryNotification);
// Issue the separate wear notifications
notificationManager.notify(notificationId + 2, notification2);
notificationManager.notify(notificationId + 1, notification1);
}
}
after that call notification in activity..
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private NotificationUtils mNotificationUtils;
private final int mNotification_Id=101;
private static final String KEY_TEXT_REPLY = "key_text_reply";
private EditText mEtChannel1, mEtChannel2,mEtGroupName;
private Button mBtnCreateChannel,mBtnDialog,mBtnSettings,mBtnGroupChannel,mBtnGroupNotification,mBtnExpandLayout,mBtnReplay,mBtndelete,mBtnGroup;
private int mImportance;
private Notification.Builder mBuilder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
initView();
setListener();
}
}
/**
* this method used to set listener on view controls.
*/
private void setListener()
{
mBtnDialog.setOnClickListener(this);
mBtnCreateChannel.setOnClickListener(this);
mBtnSettings.setOnClickListener(this);
mBtnExpandLayout.setOnClickListener(this);
mBtnReplay.setOnClickListener(this);
mBtndelete.setOnClickListener(this);
mBtnGroupChannel.setOnClickListener(this);
mBtnGroupNotification.setOnClickListener(this);
mBtnGroup.setOnClickListener(this);
}
/**
* this method initialized view controls.
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void initView() {
mNotificationUtils = new NotificationUtils(this);
mEtChannel1 = (EditText) findViewById(R.id.amEtChannel1);
mEtChannel2 = (EditText) findViewById(R.id.amEtChannel2);
mEtGroupName = (EditText) findViewById(R.id.amEtGroupName);
mBtnCreateChannel = (Button) findViewById(R.id.amBtnChannel);
mBtnGroup= (Button) findViewById(R.id.amBtnGroup);
mBtnDialog= (Button) findViewById(R.id.amBtnDialog);
mBtnSettings= (Button) findViewById(R.id.amBtnSetting);
mBtnExpandLayout= (Button) findViewById(R.id.amBtnExpandLayout);
mBtnReplay= (Button) findViewById(R.id.amBtnReplay);
mBtndelete= (Button) findViewById(R.id.amBtnDeleteChannel);
mBtnGroupChannel= (Button) findViewById(R.id.amBtnGroupChannel);
mBtnGroupNotification= (Button) findViewById(R.id.amBtnGroupNotification);
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View view) {
switch (view.getId())
{
case R.id.amBtnChannel:
createChannle();
//setNotification();
showNotificationDialog();
break;
case R.id.amBtnGroup:
createGroup();
break;
case R.id.amBtnDialog:
if (TextUtils.isEmpty(mNotificationUtils.mChannel_Id))
{
Toast.makeText(getApplicationContext(),"First Create a Notification Channel",Toast.LENGTH_SHORT).show();
}
else {
showNotificationDialog();
}
break;
case R.id.amBtnSetting:
openSettiongs();
break;
case R.id.amBtnExpandLayout:
expandLayoutNotification();
break;
case R.id.amBtnGroupChannel:
createGroupChannel();
break;
case R.id.amBtnReplay:
replayNotification("Hii Hello");
break;
case R.id.amBtnDeleteChannel:
deleteChannelId();
break;
case R.id.amBtnGroupNotification:
mNotificationUtils.showStackNotifications();
break;
}
}
/**
* this method create two channel.
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void createChannle() {
if(!TextUtils.isEmpty(mEtChannel1.getText().toString().trim())) {
mNotificationUtils = new NotificationUtils(this);
mNotificationUtils.createChannels(mEtChannel1.getText().toString().trim());
mNotificationUtils.mChannel_Id=mEtChannel1.getText().toString().trim();
mEtChannel1.setText("");
}
if (!TextUtils.isEmpty(mEtChannel2.getText().toString().trim()))
{
mNotificationUtils = new NotificationUtils(this);
mNotificationUtils.createChannels( mEtChannel2.getText().toString().trim());
mNotificationUtils.mChannel_Id_EX=mEtChannel2.getText().toString().trim();
mEtChannel2.setText("");
}
else
{
Toast.makeText(getApplicationContext(),"Name is Empty",Toast.LENGTH_SHORT).show();
}
}
/**
* this method create group of channel.
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void createGroupChannel() {
if (!TextUtils.isEmpty(mEtChannel1.getText().toString().trim())) {
mNotificationUtils = new NotificationUtils(this);
mNotificationUtils.createChannelsWithGroup(mEtChannel1.getText().toString().trim());
mEtChannel1.setText("");
}
}
/**
* this method make a group.
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void createGroup() {
mNotificationUtils = new NotificationUtils(this);
if (!TextUtils.isEmpty(mEtGroupName.getText().toString().trim())) {
mNotificationUtils.createGroup(mEtGroupName.getText().toString().trim());
mNotificationUtils.group=mEtGroupName.getText().toString().trim();
}
else
{
Toast.makeText(getApplicationContext(),"Plase Enter Group Name",Toast.LENGTH_SHORT).show();
}
}
/**
* this method delete channel_Id.
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void deleteChannelId() {
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.deleteNotificationChannel(NotificationUtils.mChannel_Id);
}
/**
* this method call expand_layout notification.
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void expandLayoutNotification() {
mBuilder =mNotificationUtils.longNotficationMessage("Who are you");
mNotificationUtils.getManager().notify(mNotification_Id, mBuilder.build());
}
/**
* this method used to open setting or read notification channel settings.
*/
private void openSettiongs() {
Intent i = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
i.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
i.putExtra(Settings.EXTRA_CHANNEL_ID, NotificationUtils.mChannel_Id);
i.putExtra(Settings.EXTRA_CHANNEL_ID, NotificationUtils.mChannel_Id_EX);
startActivity(i);
}
/**
* this method call simple notification and also calling NotificationUtils class createNotification method.
*/
private void showNotificationDialog() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
mBuilder = mNotificationUtils.createNotification("Hii how are you");
mNotificationUtils.getManager().notify(mNotification_Id, mBuilder.build());
}
}
/**
* this method set notification.
*/
@RequiresApi(api = Build.VERSION_CODES.O)
private void setNotification() {
String username = mEtChannel1.getText().toString().trim();
String password = mEtChannel2.getText().toString().trim();
if(!TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
mBuilder = mNotificationUtils.
getAndroidChannelNotification(username, "By " + password);
mNotificationUtils.getManager().notify(mNotification_Id, mBuilder.build());
}
}
/**
* this method create a inline replay notification.
* @param message
*/
@RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
private void replayNotification(String message)
{
if(!TextUtils.isEmpty(mNotificationUtils.mChannel_Id)) {
String replyLabel = getResources().getString(R.string.label);
RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
.setLabel(replyLabel)
.build();
Intent resultIntent = new Intent(this, ResultActivity.class);
PendingIntent replyPendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), resultIntent, 0);
Notification.Action action = new Notification.Action.Builder(R.mipmap.ic_launcher_round,
getString(R.string.reply_label), replyPendingIntent)
.addRemoteInput(remoteInput)
.build();
Notification.Action cancleAction = new Notification.Action.Builder(R.mipmap.ic_launcher, "Cancle", replyPendingIntent).build();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.placeholder_border);
Notification newMessageNotification =
new Notification.Builder(getApplicationContext())
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(getString(R.string.title))
.setContentText(message)
.setAutoCancel(true)
.setLargeIcon(bitmap)
.setChannelId(NotificationUtils.mChannel_Id)
.addAction(action)
.addAction(cancleAction)
.build();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//mBuilder=mNotificationUtils.replayNotification(message);
mNotificationUtils.getManager().notify(mNotification_Id, newMessageNotification);
}
}
else
{
Toast.makeText(getApplicationContext(),"Create Notification Channel",Toast.LENGTH_LONG).show();
}
}
}
above define all things notification
Answered By - Mobile Team ADR-Flutter
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.