This is my CreateNotification class. Code has been taken from the android page. http://developer.android.com/guide/topics/ui/notifiers/notifications.html
Code:
package your.notification.manager;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcel;
import android.view.View;
public class CreateNotification extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";
Intent notificationIntent = new Intent(this, NotificationReceiver.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
final int HELLO_ID = 1;
mNotificationManager.notify(HELLO_ID, notification);
}
}
Error:
05-05 12:51:38.687: E/AndroidRuntime(8629): Caused by: java.lang.IllegalArgumentException: contentView required: pkg=com.crumbs.main id=0 notification=Notification(vibrate=null,sound=null,defaults=0x0,flags=0x10)
Your Code is working fine..
Just Clean your project and run again.
Related
I'm working on an app that shows notifications and notification sounds to the user. The user can select if he wants to see them by 2 switch buttons on the settings activity. I want to open the activity Visitor when the user clicks on the notification. I wrote the following code but this doesn't happen, what should I write instead?
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;
import java.util.Set;
import androidx.annotation.RequiresApi;
import static android.app.PendingIntent.getActivity;
import static android.content.Context.NOTIFICATION_SERVICE;
import static com.example.myevents.R.drawable.notification;
public class Settings extends AppCompatActivity {
Switch simpleswitch1;
Switch simpleswitch2;
private Notification notification;
NotificationManager manager;
Notification myNotication;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
simpleswitch1 = (Switch) findViewById(R.id.simpleswitch1);
simpleswitch2 = (Switch) findViewById(R.id.simpleswitch2);
simpleswitch1.setChecked(false);
simpleswitch2.setChecked(false);
simpleswitch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#TargetApi(Build.VERSION_CODES.O)
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){{
int notifyID = 1;
String CHANNEL_ID = "my_channel_01";// The id of the channel.
CharSequence name = "channel 1";// The user-visible name of the channel.
int importance = NotificationManager.IMPORTANCE_HIGH;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
Intent intent = new Intent(Settings.this, Visitor.class);
intent.putExtra("yourpackage.notifyId", notifyID);
PendingIntent pIntent = PendingIntent.getActivity(Settings.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Create a notification and set the notification channel.
Notification notification =
new NotificationCompat.Builder(Settings.this)
.setSmallIcon(R.drawable.notification)
.setContentTitle("TITLE")
.setContentText("TEXT")
.setChannelId(CHANNEL_ID).build();
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.createNotificationChannel(mChannel);
// Issue the notification.
mNotificationManager.notify(notifyID , notification);
}
As described in the Android documentation, you need to set the intent that will fire when the user taps the notification on your builder:
NotificationCompat.Builder(Settings.this)
.setSmallIcon(R.drawable.notification)
.setContentTitle("My Events")
.setContentText("Νέα εκδήλωση κοντά σας!")
.setContentIntent(pIntent)
.setChannelId(CHANNEL_ID).build();
See : https://developer.android.com/training/notify-user/build-notification#click
I made a custom layout for my notification, it has two buttons one for play and one for pausing the music. When i click on each button a broadcast is sent to the respective class.
Now i want to keep only one button and (toggle)change the icon of the button when the user taps the button from play to pause and vice versa, I tried several things to change the icon(my real problem) but failed so far.
In PlayerActivity.java I displays the notification by calling this line.
NotificationGenerator.customBigNotification(getApplicationContext());
Here is the code of NotifcationGenerator :
package com.example.user.musicplayer;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v4.app.NotificationCompat;
import android.widget.RemoteViews;
public class NotificationGenerator {
private static final int NOTIFICATION_ID_OPEN_ACTIVITY = 1;
private static final int NOTIFICATION_ID_CUSTOM_BIG = 1;
public static void customBigNotification(Context context){
RemoteViews expandedView = new RemoteViews(context.getPackageName(), R.layout.big_notification);
NotificationCompat.Builder nc = new NotificationCompat.Builder(context);
NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent notifyIntent = new Intent(context, PlayerActivity.class);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
notifyIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notifyIntent, PendingIntent.FLAG_UPDATE_CURRENT);
String id = "mymusicplayer";
CharSequence name = "player";
String description = "player";
int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel mChannel = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
mChannel = new NotificationChannel(id, name, importance);
mChannel.setDescription(description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
nm.createNotificationChannel(mChannel);
}
nc.setContentIntent(pendingIntent);
nc.setSmallIcon(R.drawable.ic_action_play);
nc.setAutoCancel(true);
nc.setCustomBigContentView(expandedView);
nc.setContentTitle("Music Player");
nc.setContentText("Control Audio");
nc.getBigContentView().setTextViewText(R.id.textSongName, "Lorem Ipsum Dolor");
setRemoteViews(expandedView, context);
nm.notify(NOTIFICATION_ID_CUSTOM_BIG, nc.setChannelId(id).build());
}
private static void setRemoteViews(RemoteViews remoteViews, Context c) {
// call broadcast when any control of notification is clicked.
Intent playIntent = new Intent(c, PlayBroadcast.class);
PendingIntent playPendingIntent = PendingIntent.getBroadcast(c, 0, playIntent, 0);
// Using RemoteViews to bind custom layouts into Notification
remoteViews.setOnClickPendingIntent(R.id.btnPlay, playPendingIntent);
// call broadcast when any control of notification is clicked.
Intent pauseIntent = new Intent(c, PauseBroadcast.class);
PendingIntent pausePendingIntent = PendingIntent.getBroadcast(c, 0, pauseIntent, 0);
// Using RemoteViews to bind custom layouts into Notification
remoteViews.setOnClickPendingIntent(R.id.btnPause, pausePendingIntent);
}
}
Here is the code of PlayBroadCast.java : (where the broadcast from the play button is received) :
package com.example.user.musicplayer;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
public class PlayBroadcast extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
MediaPlayer mP = PlayerActivity.mediaPlayer;
if(!mP.isPlaying()){
mP.start();
}
}
}
Pardon me if the explanation is not clear, because if i might have the correct words to explain it. I would have googled it. Thanks in advance.
Try this:
expandedLayout.setInt(R.id.notif_button_play, "setImageResource", R.drawable.ic_play)
In my case the view was ImageButton, so I changed the icon through "setImageResource". You can set the function name of your view.
Below is my code,I am using cordova Push Notification,I am trying to merge multiple push notification but its give me error, for single notification its working, i am unable to find what went wrong, kindly suggest
package org.apache.cordova.firebase;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.text.TextUtils;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import java.util.Map;
import java.util.Random;
private void sendNotification(String id, String title, String messageBody, Map<String, String> data, boolean showNotification)
{
Bundle bundle = new Bundle();
int notifyID = 1;
for (String key : data.keySet()) {
bundle.putString(key, data.get(key));
}
if (showNotification) {
Intent intent = new Intent(this, OnNotificationOpenReceiver.class);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id.hashCode(), intent,
PendingIntent.FLAG_UPDATE_CURRENT);
Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setContentTitle(title)
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri);
int numMessages = 0;
//This line gives me error:error: cannot find symbol
notificationBuilder.setContentText(currentText).setNumber(++numMessages);
notificationManager.notify(notifyID, notificationBuilder.build());
} else {
bundle.putBoolean("tap", false);
FirebasePlugin.sendNotification(bundle);
}
}
It looks like currentText that is used in the setContextText method isn't instantiated anywhere in your code.
I want to create notification daily in my android app. For the purpose I made a createNotification method. Now, I want to call the method at a particular time. My mainactivity.java is as follows:
package com.example.shiza.dailyquranquote;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import java.util.Calendar;
import java.util.Date;
public class MainActivity extends ActionBarActivity {
Date startDate= new Date("03/31/2015 12:00:00");
Date endDate = new Date();
TextView textView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.verse);
long startDay = startDate.getTime() / 1000 / 60 / 60 / 24;
long endDay = endDate.getTime() / 1000 / 60 / 60 / 24;
long daysBetween = endDay - startDay;
SharedPreferences sharedPreferences = getSharedPreferences("QuranVerse",0);
String id = ""+ (daysBetween % 365) ;
String verse = sharedPreferences.getString(id, "");
if (verse == null)
{
verse ="Sufficient is Allah for me; There is no power except Him; And in Him I put my trust.";
}
Calendar rightNow = Calendar.getInstance();
textView.setText(verse + "current hour is" + rightNow.HOUR_OF_DAY);
if ( rightNow.HOUR_OF_DAY == 11 )
{
createNotification();
}
}
public void goToInsertVerse(View v)
{
Intent intent = new Intent(this,InsertVerse.class);
startActivity(intent);
}
public void goToGetVerse(View v)
{
Intent intent = new Intent(this,GetVerse.class);
startActivity(intent);
}
public void createNotification(View view) {
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("New mail from " + "test#gmail.com")
.setContentText("Subject").setSmallIcon(R.drawable.background)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
}
Now I need to pass view as a parameter to createNotification. I am unable to do it. How I can achieve it?
user setContent method wich takes "RemoteViews" as parameter
I'm diving into Java (this is day 1) and I'm trying to create a button that will trigger a notification when I click it...
This code is based off of the notification documentation here, and UI events documentation here
package com.example.contactwidget;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
public class ContactWidget extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button calc1 = (Button) findViewById(R.id.calc_button_1);
calc1.setOnClickListener(buttonListener);
setContentView(R.layout.main);
}
private static final int HELLO_ID = 1;
//Error: OnClickListener cannot be resolved to a type
private OnClickListener buttonListener = new OnClickListener() {
public void onClick (View v) {
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.icon;
CharSequence ticketBrief = "Button Pressed Brief";
CharSequence ticketTitle = "Button pressed";
CharSequence ticketText = "You pressed button 1";
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, ticketBrief, when);
Intent notificationIntent = new Intent(this, ContactWidget.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(getApplicationContext(), ticketTitle, ticketText, contentIntent);
mNotificationManager.notify(HELLO_ID, notification);
}
}
}
I'm running into a problem: OnClickListener cannot be resolved to a type. The problem here is that I don't see any problems with my code in relation to the example I'm using
Add this import:
import android.view.View.OnClickListener;
If you are using Eclipse, you can use Ctrl+Shift+O to make it import those clases or interfaces automagically.
Make sure you have both these imports:
import android.view.View;
import android.view.View.OnClickListener;
setContentView(R.layout.main);
Should be above the button declaration, just below
super.onCreate(savedInstanceState);