Last active
October 20, 2016 11:16
-
-
Save Sp4Rx/5c517bad69812a69219a9018f60ef086 to your computer and use it in GitHub Desktop.
ServiceDemo 0.3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.suvajit.servicedemo; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.os.IBinder; | |
import android.support.annotation.Nullable; | |
import android.widget.Toast; | |
public class BackgroundService extends Service { | |
@Nullable | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
Toast.makeText(BackgroundService.this, "Service Started...", Toast.LENGTH_SHORT).show(); | |
return START_STICKY; | |
} | |
@Override | |
public void onDestroy() { | |
// Removed the restart service from onDestroy | |
Toast.makeText(BackgroundService.this, "Service is Stopping...", Toast.LENGTH_SHORT).show(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.suvajit.servicedemo; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.Toast; | |
import com.google.firebase.iid.FirebaseInstanceId; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Intent intentService = new Intent(this,BackgroundService.class); | |
startService(intentService); | |
String token = FirebaseInstanceId.getInstance().getToken(); | |
Log.i("MainActivity: FB Token",token); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.example.suvajit.fcmnotification"> | |
<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> | |
<service android:name=".BackgroundService" android:exported="false" /> | |
<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> | |
</manifest> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.suvajit.servicedemo; | |
import android.util.Log; | |
import com.google.firebase.iid.FirebaseInstanceId; | |
import com.google.firebase.iid.FirebaseInstanceIdService; | |
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService { | |
@Override | |
public void onTokenRefresh() { | |
super.onTokenRefresh(); | |
String newToken = FirebaseInstanceId.getInstance().getToken(); | |
Log.d("FBInstanceIdService",newToken); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.suvajit.servicedemo; | |
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.support.v4.app.NotificationCompat; | |
import com.google.firebase.messaging.FirebaseMessagingService; | |
import com.google.firebase.messaging.RemoteMessage; | |
public class MyFirebaseMessagingService extends FirebaseMessagingService { | |
@Override | |
public void onMessageReceived(RemoteMessage remoteMessage) { | |
super.onMessageReceived(remoteMessage); | |
if(remoteMessage.getNotification() != null){ | |
Intent intent = new Intent(this,BackgroundService.class); | |
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT); | |
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) | |
.setContentTitle("Push Message") | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setContentText("This is a message") | |
.setContentIntent(pendingIntent); | |
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.notify(0,notificationBuilder.build()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment