내 안드로이드 응용 프로그램에서 3 개 활동이 :항상 시작의 주요 활동 의도
는1. Main
2. Welcome
3. Articles
애플리케이션의 라이프 사이클은 홈페이지에서 시작 -> 시작 -> 제. 필자는 애플리케이션에서 Firebase Cloud Messaging (FCM)을 구현하여 환영 또는 기사 활동을 시작하고자합니다. 코드는 다음과 같이 진행됩니다
private void createNote(String title, String body, String key) {
try {
Intent screen;
int code = key == null ? 0 : Integer.parseInt(key);
if (key == null) {
screen = new Intent(this, Welcome.class);
} else {
screen = new Intent(this, Articles.class);
screen.putExtra("key", key);
screen.putExtra("name", title);
}
screen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
screen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(
this, 0, screen, PendingIntent.FLAG_CANCEL_CURRENT);
//
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setTicker(title + " has new Item")
.setSmallIcon(R.drawable.logo)
.setContentIntent(intent)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(code, notification.build());
} catch (Exception ex) {
ex.printStackTrace();
Log.e(TAG + "NOTE", ex.getMessage());
}
}
응용 프로그램 블록을 매니페스트 파일에서
<application
android:allowBackup="true"
android:icon="@mipmap/logo"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<activity android:name=".infotropy.Main" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".infotropy.Welcome"
android:screenOrientation="portrait"
android:launchMode="singleInstance"/>
<activity android:name=".infotropy.Articles" android:screenOrientation="portrait"/>
</application>
내 문제입니다 활동 클래스 참조가 푸시 알림을 클릭 한 후, 변수 대기 화면에 상관없이 항상 홈페이지 액티비티이 시작되었는데, 나는 언급 된 곳에서 언급하지 않았다.
코드는 시작해야 하나 는 또는 기사 활동에 오신 것을 환영합니다,하지만 항상 홈페이지을 활성화합니다. 해결책을주십시오.
편집 : 요청으로
, 내가 FCM의 푸시 알림을 처리하는 클래스 코드를 공유하고있다 :
public class Notify extends FirebaseMessagingService {
private final String TAG = "NOTIFY_";
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
//super.onMessageReceived(remoteMessage);
try {
String key = null;
Map data = remoteMessage.getData();
String body = remoteMessage.getNotification().getBody();
String title = remoteMessage.getNotification().getTitle();
Log.d(TAG, "From : " + remoteMessage.getFrom());
Log.d(TAG, "Body : " + body);
if (data.containsKey("key")) {
key = data.get("key").toString();
}
createNote(title, body, key);
} catch (Exception ex) {
ex.printStackTrace();
Log.e(TAG + "RECEIVER", ex.getMessage());
}
}
private void createNote(String title, String body, String key) {
try {
Intent screen;
int code = key == null ? 0 : Integer.parseInt(key);
if (key == null) {
screen = new Intent(this, Welcome.class);
} else {
screen = new Intent(this, Articles.class);
screen.putExtra("key", key);
screen.putExtra("name", title);
}
screen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
screen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Intent welcome = new Intent(this, Welcome.class);
// welcome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent intent = PendingIntent.getActivity(
this, 0, screen,
PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT);
//
NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
.setTicker(title + " has new Item")
.setSmallIcon(R.drawable.logo)
.setContentIntent(intent)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(code, notification.build());
} catch (Exception ex) {
ex.printStackTrace();
Log.e(TAG + "NOTE", ex.getMessage());
}
}
}
@VihangaYasith합니다. 기사 활동에는 서버의 최신 데이터를 동기화하는 메커니즘이 있기 때문입니다. 따라서 푸시 알림이 기사를 나타낼 때마다 모든 이전 활동을 지우고 기사에서 서버의 최신 데이터를 동기화하는 새 인스턴스를 갖습니다. –
FCM 메시지 처리기의 전체 코드를 공유하십시오. –
@AGMTazim. 전체 코드를 추가했습니다. 제발 봐 –