Last active
June 18, 2019 04:04
-
-
Save flatfisher/a43e6e84558446e5f6b32d73f4f66eb3 to your computer and use it in GitHub Desktop.
AndroidのPush通知(FCM)をサーバー知識無しで試してみよう ref: https://qiita.com/flatfisher/items/bdec83caf3c7f9c8917c
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
<application> | |
<service | |
android:name=".MyFcmListenerService"> | |
<intent-filter> | |
<action android:name="com.google.firebase.MESSAGING_EVENT" /> | |
</intent-filter> | |
</service> | |
<application/> |
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
dependencies { | |
compile "com.google.firebase:firebase-messaging:10.2.4" | |
} | |
// ファイルの一番下に追加 | |
apply plugin: 'com.google.gms.google-services' |
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
keytool -exportcert -list -v \ | |
-alias androiddebugkey -keystore ~/.android/debug.keystore |
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
MyInstanceIDListenerService: Refreshed token: xxxxxxxxxxxxxxxxxxxxx |
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
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import com.google.firebase.messaging.FirebaseMessaging; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
//Push通知の購読開始 | |
FirebaseMessaging.getInstance().subscribeToTopic("mytopic"); | |
//購読解除 | |
FirebaseMessaging.getInstance().unsubscribeFromTopic("mytopic"); | |
} | |
} |
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
import android.app.NotificationManager; | |
import android.app.PendingIntent; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.media.RingtoneManager; | |
import android.net.Uri; | |
import android.support.v4.app.NotificationCompat; | |
import android.util.Log; | |
import com.google.firebase.messaging.FirebaseMessagingService; | |
import com.google.firebase.messaging.RemoteMessage; | |
import java.util.Map; | |
public class MyFcmListenerService extends FirebaseMessagingService { | |
private final static String TAG = MyFcmListenerService.class.getSimpleName(); | |
@Override | |
public void onMessageReceived(RemoteMessage message){ | |
String from = message.getFrom(); | |
Map data = message.getData(); | |
Log.d(TAG, "from:" + from); | |
Log.d(TAG, "data:" + data.toString()); | |
String msg = data.get("data").toString(); | |
sendNotification(msg); | |
} | |
private void sendNotification(String message) { | |
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_RINGTONE); | |
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this) | |
.setSmallIcon(R.mipmap.ic_launcher) | |
.setContentTitle("Push通知のタイトル") | |
.setSubText("Push通知のサブタイトル") | |
.setAutoCancel(true) | |
.setSound(defaultSoundUri) | |
.setStyle(new NotificationCompat.BigTextStyle().bigText(message)) | |
.setContentIntent(pendingIntent); | |
NotificationManager notificationManager = | |
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); | |
notificationManager.notify(0 , notificationBuilder.build()); | |
} | |
} |
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
import android.util.Log; | |
import com.google.firebase.iid.FirebaseInstanceId; | |
import com.google.firebase.iid.FirebaseInstanceIdService; | |
public class MyInstanceIDListenerService extends FirebaseInstanceIdService { | |
private static final String TAG = MyInstanceIDListenerService.class.getSimpleName(); | |
@Override | |
public void onTokenRefresh() { | |
//ここで取得したInstanceIDをサーバー管理者に伝える | |
String refreshedToken = FirebaseInstanceId.getInstance().getToken(); | |
Log.d(TAG, "Refreshed token: " + refreshedToken); | |
} | |
} |
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
keytool -exportcert -list -v \ | |
-alias androiddebugkey -keystore %USERPROFILE%\.android\debug.keystore |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment