I am trying to create a custom solution, to notify on SMS Receive.
I found some solutions on Stackoverflow, but they are not working, can anybody help me, where i am wrong. Below is my code.
public class MainActivity extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast toast = Toast.makeText(context,"Message Received", Toast.LENGTH_LONG);
toast.show();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("My notification")
.setContentText("SMS Received");
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1, mBuilder.build());
}
}
and Manifest file:
<receiver android:name=".MainActivity"
android:enabled="true"
android:exported="true"
>
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
Try this:
<receiver
android:name=".IncomingSmsReceiver"
android:permission="android.permission.BROADCAST_SMS"
android:priority="1000">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
public class IncomingSmsReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
if (Telephony.Sms.Intents.SMS_RECEIVED_ACTION.equals(intent.getAction())) {
String messageBody = "no data";
String messageFrom = "no data";
for (SmsMessage smsMessage : Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
messageBody = smsMessage.getMessageBody();
messageFrom = smsMessage.getDisplayOriginatingAddress();
}
Log.d("Sms:", messageBody + " " + messageFrom);
}
}
}
Related
I try to start a ForegroundService by an BroadcastReceiver on Boot. I have tried this on a device with Android 8 and on an emulated Android 10 device. Both work fine. The problem is, that this code does not work on my Android 10 Galaxy S9. I have debugged everything, my BroadcastReceiver and the OnStartCommand method but also if the BroadcastReceiver runs the code fine, the OnStartCommand method does not start running.
I have a mainActivity in which the user has to apply this Process so the app gets opened once before the BroadcastReceiver should work.
I tried almost every solution i found here but nothing worked for me.
I don't understand why this is working on some devices but not on each. I hope someone can help me!
My min API is 26.
BroadcastReceiver
public class Restarter extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
Intent intentt = new Intent (context, MyService.class);
intentt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startForegroundService(intentt);
} else {
Intent intentt = new Intent (context, MyService.class);
intentt.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startService(intentt);
}
}
}
Service
public class MyService extends Service {
public static final String CHANNEL_ID = "exampleServiceChannel";
#Override
public void onCreate() {
super.onCreate();
Log.d("WhereAreWe", "create");
createNotificationChannel();
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel serviceChannel = new NotificationChannel(
CHANNEL_ID,
"Foreground Service Channel",
NotificationManager.IMPORTANCE_DEFAULT
);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
Log.d("WhereAreWe", "StartCommand");
SharedPreferences check = getSharedPreferences("Checked", Context.MODE_PRIVATE);
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O){
Intent intents = new Intent(this, MainActivity.class);
intents.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intents, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "newid")
.setSmallIcon(R.drawable.ic_android)
.setContentTitle("Foreground Service")
.setContentText("Example text")
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent);
startForeground(1, builder.build());
}else{
startForeground(1, new Notification());
}
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.MyApplication">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application
.
.
.
<receiver
android:name=".Restarter"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="restartservice" />
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<service
android:name=".MyService"
android:enabled="true"
android:exported="true"
android:stopWithTask="false" />
</application>
</manifest>
I am working on Android app.Firstly, I am not familiar with BroadcastReceiver. I need to create an app in which, if install app have specific package like "com.whatsapp" a broadcast receiver will show the Toast
public class PackageAddedReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Package Installed: ", Toast.LENGTH_LONG).show();
}
}
Manifest
<receiver android:name=".receiver.PackageAddedReceiver" android:label="Package added Receiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
How can I add BroadcastReceiver for the specific package?
Try this
public class PackageAddedReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Uri data = intent.getData();
String mypkg="package:com.pck.name";
Log.e("DATA",data+"");
Log.e( "Action: " ,intent.getAction());
if(mypkg.equals(data.toString())){
Toast.makeText(context, "Package Installed: ", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context, "not match ", Toast.LENGTH_LONG).show();
}
}
Manifest code
<receiver android:name=".PackageAddedReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_INSTALL" />
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package"/>
</intent-filter>
</receiver>
A. Create BroadcastReceiver class (implement onReceive): you can extract the data related to package
import android.content.*;
import android.net.Uri;
import android.util.Log;
public class PackageChangeReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context ctx, Intent intent) {
Uri data = intent.getData();
Log.d(TAG, "Action: " + intent.getAction());
Log.d(TAG, "The DATA: " + data);
}
}
B. Declare receiver with intent-filter in AndroidManifest.xml:
<receiver android:name="PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED"/>
<action android:name="android.intent.action.PACKAGE_REPLACED"/>
<action android:name="android.intent.action.PACKAGE_REMOVED"/>
<data android:scheme="package"/>
</intent-filter>
</receiver>
The receiver should be registered programmatically as below :
val packageAddedReceiver = PackageAddedReceiver()
val intentFilter = IntentFilter()
intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED)
intentFilter.addAction(Intent.ACTION_PACKAGE_FIRST_LAUNCH)
intentFilter.addAction(Intent.ACTION_PACKAGE_INSTALL)
intentFilter.addDataScheme("package")
registerReceiver(packageAddedReceiver, intentFilter)
for further explanation check CommonsWare answer in the link
Can't receive broadcasts for PACKAGE intents
Here is my full code, in this. If the app is running, then i'll get push notification that too for public void handleResponse method. But if i quit the app, i wont get push notification itself, please help.
Mainactivity.java
public class MainActivity extends AppCompatActivity {
private static final String YOUR_APP_ID = "2C25D658-240D-4350-FF8A-6CF4DBD88F00";
private static final String YOUR_SECRET_KEY = "24DB6F44-8C97-1DCF-FF22-060777780600";
String appVersion = "v1";
Subscription subscription;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Backendless.initApp(this, YOUR_APP_ID, YOUR_SECRET_KEY, "v1");
Backendless.Messaging.registerDevice("183254839430", "default", new AsyncCallback<Void>() {
#Override
public void handleResponse(Void aVoid) {
Toast.makeText(MainActivity.this, "Registered", Toast.LENGTH_LONG).show();
}
#Override
public void handleFault(BackendlessFault backendlessFault) {
Toast.makeText(MainActivity.this, backendlessFault.getMessage(), Toast.LENGTH_LONG).show();
}
});
Backendless.Messaging.subscribe( "default",
new AsyncCallback<List<Message>>() {
#Override
public void handleResponse(List<Message> response) {
Toast.makeText(MainActivity.this, "Notification arrived successfully", Toast.LENGTH_LONG).show();
for (Message message : response) {
String publisherId = message.getPublisherId();
Object data = message.getData();
}
}
#Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(MainActivity.this, fault.getMessage(), Toast.LENGTH_SHORT).show();
}
},
new AsyncCallback<Subscription>()
{
#Override
public void handleResponse( Subscription response )
{
subscription = response;
}
#Override
public void handleFault( BackendlessFault fault )
{
Toast.makeText( MainActivity.this, fault.getMessage(), Toast.LENGTH_SHORT ).show();
}
}
);
}
}
pushreceiver.java.
This code itself is not triggering, i'm confused why :-(
public class PushReceiver extends GcmListenerService {
//This method will be called on every new message received
#Override
public void onMessageReceived(String from, Bundle data) {
Toast.makeText(getApplicationContext(), "Notification came PushReceiver", Toast.LENGTH_LONG).show();
//Getting the message from the bundle
String message = data.getString("message");
//Displaying a notiffication with the message
sendNotification(message);
}
//This method is generating a notification and displaying the notification
private void sendNotification(String message) {
Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
int requestCode = 0;
PendingIntent pendingIntent = PendingIntent.getActivity(this, requestCode, intent, PendingIntent.FLAG_ONE_SHOT);
Uri sound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder noBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentText(message)
.setAutoCancel(true)
.setContentIntent(pendingIntent);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, noBuilder.build()); //0 = ID of notification
}
}
My Manifest file.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="feelzdroid.backednlessgcm">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<uses-permission android:name="feelzdroid.backednlessgcm.permission.C2D_MESSAGE"/>
<permission android:name="feelzdroid.backednlessgcm.permission.C2D_MESSAGE" android:protectionLevel="signature"/>
<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="com.google.android.gms.gcm.GcmReceiver"
android:exported="true"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
<action android:name="com.google.android.c2dm.intent.REGISTRATION"/>
<category android:name="feelzdroid.backednlessgcm"/>
</intent-filter>
</receiver>
<service android:name=".PushReceiver" android:exported="false">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE"/>
</intent-filter>
</service>
</application>
Log.
07-03 16:43:31.893 11026-11143/feelzdroid.backednlessgcm I/GMPM: App measurement is starting up
07-03 16:43:31.915 11026-11143/feelzdroid.backednlessgcm E/GMPM: getGoogleAppId failed with status: 10
07-03 16:43:31.918 11026-11143/feelzdroid.backednlessgcm E/GMPM: Uploading is not possible. App measurement disabled
I was working a little with push notifications, which I managed to put working so far and with the Firebase Cloud Messaging Api, now I was trying to save the Token in the database, was following a tutorial and the token doesn't seem to register.
MainActivity.java
http://prntscr.com/bgadg6
MyFirebaseInstanceIDService.java
http://prntscr.com/bgadxd
MyFirebaseMessagingService extends Firebase Messaging Service{
private static final String TAG = "MyFirebaseMsgService";
#Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//Displaying data in log
//It is optional
Log.d(TAG, "From: " + remoteMessage.getFrom());
Log.d(TAG, "Notification Message Body: " + remoteMessage.getNotification().getBody());
//Calling method to generate notification
sendNotification(remoteMessage.getNotification().getBody());
}
//This method is only generating push notification
//It is same as we did in earlier posts
private void sendNotification(String messageBody) {
Intent intent = new Intent(this, MainActivity.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_launcher)
.setContentTitle("Firebase Push Notification")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Adding Internet Permission -->
<uses-permission android:name="android.permission.INTERNET"/>
<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>
<!--
Defining Services
-->
<service
android:name=".MyFirebaseMessagingService">
<intent-filter>
<action android:name="com.google.firebase.MESSAGING_EVENT"/>
</intent-filter>
</service>
<service
android:name=".MyFirebaseInstanceIDService">
<intent-filter>
<action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
</intent-filter>
</service>
</application>
Try this, with volley library & asyntask:
//Creating a string request
StringRequest req = new StringRequest(Request.Method.POST, Constants.REGISTER_URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//dismissing the progress dialog
progressDialog.dismiss();
//if the server returned the string success
if (response.trim().equalsIgnoreCase("success")) {
//Displaying a success toast
Toast.makeText(MainActivity.this, "Registered successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Choose a different email", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
//adding parameters to post request as we need to send firebase id and email
params.put("firebaseid", uniqueId);
params.put("email", email);
return params;
}
and register.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
//getting the request values
$firebaseid = $_POST['firebaseid'];
$email = $_POST['email'];
//connecting to database
$con = mysqli_connect('localhost','root','','pushnotification');
//creating an sql query
$sql = "INSERT INTO register (firebaseid, email) VALUES ('$firebaseid','$email')";
//executing the query to the database
if(mysqli_query($con,$sql)){
echo 'success';
}else{
echo 'failure';
}
}
This is my first time trying to make an app that starts on boot and runs in the background, and I'm finding all the possible ways of doing it a little confusing. I need the app to do a quick check of a few things every half hour, and sleep in between, so I settled for using AlarmManager and setRepeating.
In this test I've set it up so a boot receiver sets the alarm, which should run a service every 60 seconds (actual app will be 30+ mins between running this). The service triggers a notification, but when I reboot the phone, the notification only shows up once. I clear it and never see it again.
I'll post the relevant code below. Can anyone tell me where I'm going wrong? Thanks!
AlarmStarter.java
public class AlarmStarter extends BroadcastReceiver {
private static final int PERIOD = 60000;
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
scheduleAlarms(context);
}
}
static void scheduleAlarms(Context ctxt) {
AlarmManager mgr = (AlarmManager) ctxt.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(ctxt, BackService.class);
PendingIntent pi = PendingIntent.getService(ctxt, 0, i, 0);
mgr.setRepeating(AlarmManager.ELAPSED_REALTIME,
SystemClock.elapsedRealtime() + PERIOD, PERIOD, pi);
}
BackService.java
public class BackService extends Service {
public BackService() {
//showNotification("ticker test", "title test", "content test", "http://google.com");
Log.d("CF", "Starting BackService");
showNotification("ticker test", "title test", "content test", "http://google.com");
}
#Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
#Override
public void onCreate() {
// Code to execute when the service is first created
}
public void showNotification(String ticker, String title, String content, String url) {
Intent notificationIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this).setTicker(ticker)
.setSmallIcon(android.R.drawable.ic_menu_report_image)
.setContentTitle(title)
.setContentText(content)
.setContentIntent(pi)
.setAutoCancel(true)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notification);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.testapp.test" >
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver
android:name=".AlarmStarter"
android:enabled="true" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<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>
<service
android:name=".BackService"
android:enabled="true"
android:exported="true" ></service>
</application>
</manifest>
The problem I had here was that the BackService class I was trying to trigger with the alarm, was extending from the Service class. It should have extended from the BroadcastReceiver class, as only BroadcastReceivers can be triggered by the externally sent alarms.