First of all I would like to thank you for clicking on this thread.
Thank you so much for taking your time to read this.
I was planning for the application push a notification on a specific time.
I followed some tutorials online but it seems it doesn't work.
There is no error message on the code itself but no notification is coming out.
Here's my "Notification_receiver.java" file:
package com.example.reviewerapplication;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
public class Notification_receiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent repeating_intent = new Intent(context,Chapterchoice.class);
String daily10 = "daily";
intent.putExtra("daily",daily10);
repeating_intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(context,100,repeating_intent,PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setContentIntent(pendingIntent)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle("Sample Title")
.setContentText("same message")
.setAutoCancel(true);
if (intent.getAction().equals("MY_NOTIFICATION_MESSAGE")) {
notificationManager.notify(100, builder.build());
}
}
}
This is what is on my MainActivity:
//Daily Notification
val calendar = Calendar.getInstance()
calendar.set(Calendar.HOUR_OF_DAY,21)
calendar.set(Calendar.MINUTE,53)
calendar.set(Calendar.SECOND,0)
val intent2 = Intent(this, Notification_receiver::class.java)
intent2.setAction("MY_NOTIFICATION_MESSAGE")
var pendingIntent = PendingIntent.getBroadcast(this,100,intent2,PendingIntent.FLAG_UPDATE_CURRENT )
var alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,calendar.timeInMillis,AlarmManager.INTERVAL_DAY,pendingIntent)
I actually got it fixed just now.
my issue is that I used "activity" in the Manifest instead of using "receiver"
Related
Hey I am just learning to Android development so I am trying to make push notifications. Book I read says to build it like that. But android studio says "Builder is deprecated" about NotificationCompat.Builder(this)
I also tried to find information on the internet, but none of it works.
This is my code:
package com.example.notifications;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.core.app.TaskStackBuilder;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
public static final int NOTIFICATION_ID = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onClick(View view){
NotificationCompat.Builder mBuilder =
(NotificationCompat.Builder) new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setContentText("bioba")
.setContentTitle("aboba");
Intent resultIntent = new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(MainActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
}
}
I use emulator of Android 8.1
Use public Builder(#NonNull Context context, #NonNull String channelId) instead, where channelId is the NotificationChannel where the constructed Notification will be posted.
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;
}
}
}
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
Updated MyService #CommonsWare
i have a service that if started will set up an alarm that triggers the notification.
This works fine and the alarms are canceled if the service is stopped.
i am trying to get the notification to open a new activity class but can not get it done
my service class is the following:
package com.example.andtip;
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.v4.app.NotificationCompat;
public class BReceiver extends BroadcastReceiver {
private static final int MY_NOTIFICATION_ID=1;
NotificationManager notificationManager;
Notification myNotification;
public void onReceive(Context context, Intent intent) {
Intent myIntent = new Intent(context, DoSomething.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, new Intent("com.example.andtip"),0 );
PendingIntent pi2 = PendingIntent.getActivity(context, 0, myIntent,0 );
myNotification=new NotificationCompat.Builder(context)
.setContentTitle("This is a notification from the example alarm application.")
.setContentText("Notification")
.setTicker("Notification!")
.setWhen(System.currentTimeMillis())
.setContentIntent(pi)
.setDefaults(Notification.DEFAULT_SOUND)
.setAutoCancel(true)
.setSmallIcon(R.drawable.ic_launcher)
.build();
notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
}
how should i link the intent pi2 to the notification?
i am trying to get the notification to open a new activity class but can not get it done
You are using getBroadcast() to create your PendingIntent. Use getActivity() to create a PendingIntent that starts up an activity. Make sure that the Intent you put in the PendingIntent is for an activity, and make sure that the activity has its entry in the manifest.
I put here my code...
I explain my problem..
when I create a notification, it appears to me immediately even if I enter a specific time to make it appear.
at that time instead opens automatically as if i push on notification.
can you tell me how I can play it at the time that i inserted?
thanks
import java.util.Calendar;
import java.util.StringTokenizer;
import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
public class Allarm {
private Context context;
public Allarm(Context context) {
this.context = context;
}
public void createAlarm(String rip,String title,String object,String dati) {
String day= "01";
String month= "01";
String year= "2014";
String hour= "00";
String minute = "02";
String date = day+"/"+month+"/"+year+" "+hour+":"+minute;
Calendar myAlarmDate = Calendar.getInstance();
myAlarmDate.setTimeInMillis(System.currentTimeMillis());
myAlarmDate.set(Integer.valueOf(year), Integer.valueOf(month),
Integer.valueOf(day), Integer.valueOf(hour),
Integer.valueOf(minute), 0);
AlarmManager alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent intent =new Intent(context, NotificationReceiverActivity.class);
intent.putExtra("dati", dati);
intent.setData(Uri.parse("content://"+date));
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager notificationManager =(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
context)
.setWhen(myAlarmDate.getTimeInMillis())
.setContentText(object)
.setContentTitle(title)
.setSmallIcon(R.drawable.avvio)
.setAutoCancel(true)
.setTicker(title)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_VIBRATE| Notification.DEFAULT_SOUND)
.setContentIntent(pendingIntent);
Notification notification=notificationBuilder.build();
alarmManager.set(AlarmManager.RTC_WAKEUP,
myAlarmDate.getTimeInMillis(), pendingIntent);
notificationManager.notify((int) myAlarmDate.getTimeInMillis(), notification);
}
}
The method NotificationManager.notify(int id, Notification notification) posts the notification immediately. The first argument is an id you specify and not the time the notification should be displayed.
You could use the AlarmManager to schedule a notification.