Es Depende de lo que necesites para tu proyecto. pero principalmente es que necesite:
- "Service class" : realizar alguna tarea en el fondo mientras no se utiliza la aplicación.
- "BroadcastReceiver" : mostrar la notificación com tu quieras cuando obtenga la información requerida de "Service Class".
Aquí hay un ejemplo de BroadcastReceiver para mostrar una notificación cuando recibe un SMS. Aquí no he creado una clase de Service porque no es necesario. Ya está creado por android. y puedo obtener los SMS.
primero crear una clase extiende BroadcastReceiver:
public class pullData extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(SMS_RECEIVED)) {
// notification received
Log.d("result" , "received");
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("Received")
.setContentText("Received Successfully")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(0, builder.build());
}
}
}
luego en AndroidManifest.xml add this line <receiver android:name=".pullData" android:enabled="true"><intent-filter android:priority="2147483647"><action android:name="android.provider.Telephony.SMS_RECEIVED" /></intent-filter></receiver>
como asi :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.elmaravilla.stackoverflowtest">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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=".pullData" android:enabled="true">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
y en MainActivity.java necesitas pedir permiso para leer SMS:
public class MainActivity extends AppCompatActivity {
private static final int PERMISSIONS_REQUEST_RECEIVE_SMS = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions(Manifest.permission.RECEIVE_SMS, PERMISSIONS_REQUEST_RECEIVE_SMS);
}
private void requestPermissions(String permission, int requestCode) {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(this, permission)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
Toast.makeText(this, "Granting permission is necessary!", Toast.LENGTH_LONG).show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{permission},
requestCode);
// requestCode is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_RECEIVE_SMS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
}
Asi puedes enviar notificación al usuario cuando reciba sms.
Espero que lo entiendas bien.